java.util.scanner

Using scanner to read string from text file and then print out

♀尐吖头ヾ 提交于 2019-12-11 19:47:36
问题 I have a text file which has the string, - "!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========" in it as you can see in the code below. IF the text file contains this string I need to read it from the text file and then print it out again. The problem is I cant work out why my code is not printing it. Any help would be appreciated thanks! public class Main { public static void main(String[] args) throws FileNotFoundException { File file = new File("C:/Users/Anton

java.util.NoSuchElementException: Read words from a file

我只是一个虾纸丫 提交于 2019-12-11 18:35:06
问题 what`s wrong with this simple java program? I am trying to read words from a file and get the following exception: complication dilemma lavender issue happy weird Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at com.recrawl.util.ReadWordsTextfile.readWordsFromTextFile(ReadWordsTextfile.java:32) at com.recrawl.util.ReadWordsTextfile.main(ReadWordsTextfile.java:13) As you can see I get the

Reading Structured File in Java

萝らか妹 提交于 2019-12-11 18:35:05
问题 I have written code for reading a structured file in Java, and extracting the line contents to create objects (of type Person). The file has the following structure: [personNumber], [personName], [personAge], for example 0, Sherlock Holmes, 44 1, Harry Potter, 17 2, Jack Sparrow, 50 ... The code for reading the file and extracting the information is: RandomAccessFile rf = new RandomAccessFile(fileName, "rw"); String line; File myFile = new File(fileName); scan = new Scanner(myFile); String[]

Scanner never stops reading input

不打扰是莪最后的温柔 提交于 2019-12-11 18:21:43
问题 I'm simply trying to read integers (separated by spaces, such as : "6 9 20") from the console and transferring them into an array for later use. However, with the code I currently have written, my scanner object never stops reading my input. I feel like the scanner object simply doesn't know exactly when to stop taking input, but I'd like it to stop reading as soon as I hit Enter. I've searched this website extensively for a precise answer, but to no avail. Here is my code: int[] mcNug = new

file not found error when saving txt file

China☆狼群 提交于 2019-12-11 17:51:21
问题 I have a problem with a file saving alogrithm. error: dec 11, 2012 4:56:18 PM tetris.FileIO loadHighscores SEVERE: null java.io.FileNotFoundException: file:\C:\Users\Koen\Dropbox\2TI\vgo\Eindwerk\Tetris\build\classes\tetris\LineHighscores.txt (De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.util.Scanner.<init>(Scanner.java:656) at tetris.FileIO.loadHighscores

Java - combine Scanner for file with Scanner for user input

可紊 提交于 2019-12-11 15:48:53
问题 Edit2: See below for some example code, this is left because people did reply to this question in its original form. Here's one from myself and my CS professor combined: We have an assignment where students are being asked to write a basic command interface for a modified subset of SQL. Doesn't really matter, but provides context. The requirements state that commands can be in a file or entered at a command prompt. Scanner seems natural for this, fairly obviously. So for this question, I'll

Take string input, parse each word to all lowercase and print each word on a line, non-alphabetic characters are treated as a break between words

醉酒当歌 提交于 2019-12-11 15:28:35
问题 I'm trying to take a string input, parse each word to all lowercase and print each word on a line (in sorted order), ignoring non-alphabetic characters (single letter words count as well). So, Sample input: Adventures in Disneyland Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left." So they went home. Output: a adventures blondes came disneyland fork going home in left read road sign so the they to two went were when My program: Scanner

Java read line until followstop

南楼画角 提交于 2019-12-11 14:20:49
问题 Actually, I am trying to read file which contain of multiple lines. for this I am using scanner.nextline() However, I want to read the line until the followstop (dot separator) which usually followed by space or end of line char. May any body help me in this case ? 回答1: If you want to search until a period, you can use a Matcher with a Pattern . //Pattern p = Pattern.compile("[^\\.]*\\.(\\s+)"); Pattern p = Pattern.compile(".*?\\.(\\s+)"); //Anything any amount of times, //followed by a dot

Input using thread

主宰稳场 提交于 2019-12-11 13:03:33
问题 import java.util.Scanner; public class ThreadClass { public static void main(String[] args) { System.out.println("Type the following in 5 seconds"); System.out.println("The quick brown fox jumps over the lazy dog"); try { Scanner sc = new Scanner(System.in); String str=sc.nextLine(); Thread.sleep(1000); System.out.println("Your time is over"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } In the above program, I want that my program should

scanner.hasNext() results in an infinite loop

余生颓废 提交于 2019-12-11 12:29:15
问题 I have a piece of code which should read input word by word and add the length of the word to an ArrayList recursively (for a school exercise). I wrote this method: ArrayList<Integer> wordLengths = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); // ... private void readWords() { // read a word, if there are more words, read the next word this.wordLengths.add(this.scanner.next().length()); if (this.scanner.hasNext()) { readWords(); } } When I call this method, it keeps