java.util.scanner

Java Scanner class reading strings [duplicate]

微笑、不失礼 提交于 2019-12-30 10:22:59
问题 This question already has answers here : Java Scanner class reading strings (5 answers) Closed 5 years ago . I got the following code: int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.next(); } System.out.println(names[0]); When I run this code, the scanner will only pick up the

Java read txt file to hashmap, split by “:”

扶醉桌前 提交于 2019-12-30 07:27:12
问题 I have a txt file with the form: Key:value Key:value Key:value ... I want to put all the keys with their value in a hashMap that I've created. How do I get a FileReader(file) or Scanner(file) to know when to split up the keys and values at the colon (:) ? :-) I've tried: Scanner scanner = new scanner(file).useDelimiter(":"); HashMap<String, String> map = new Hashmap<>(); while(scanner.hasNext()){ map.put(scanner.next(), scanner.next()); } 回答1: Read your file line-by-line using a

Infinite While Loop When InputMidmatchException is caught in try-catch block [duplicate]

不羁岁月 提交于 2019-12-29 08:55:35
问题 This question already has answers here : try/catch with InputMismatchException creates infinite loop (7 answers) Closed 4 years ago . I keep getting my code caught in an infinite while loop. It is nothing to advanced, but i can not figure it out for the life of me! Someone Please help I have purplosely just re created the specific error without all of the if statements i have in my actual program. package bs; import java.util.InputMismatchException; import java.util.Scanner; public class bs {

Java - Parsing Text File

Deadly 提交于 2019-12-29 07:56:11
问题 I have an input text file in this format: <target1> : <dep1> <dep2> ... <target2> : <dep1> <dep2> ... ... And a method that takes two parameters function(target, dep); I need to get this parsing to call my method with each target and dep eg: function(target1, dep1); function(target1, dep2); function(target1, ...); function(target2, dep1); function(target2, dep2); function(target2, ...); What would be the most efficient way to call function(target,dep) on each line of a text file? I tried

java.util.scanner throws NoSuchElementException when application is started with gradle run

拜拜、爱过 提交于 2019-12-29 06:14:24
问题 I have a created a simple java "echo" application that takes a user's input and shows it back to them to demonstrate the issue. I can run this application without trouble using IntelliJ's internal "run" command, and also when executing the compiled java file produced by gradle build . However, if I try to execute the application using gradle run , I get a NoSuchElementException thrown from the scanner. I think gradle or the application plugin specifically is doing something strange with the

Java Scanner question

。_饼干妹妹 提交于 2019-12-28 13:38:11
问题 How do you set the delimiter for a scanner to either ; or new line? I tried: Scanner.useDelimiter(Pattern.compile("(\n)|;")); But it doesn't work. 回答1: As a general rule, in patterns, you need to double the \ . So, try Scanner.useDelimiter(Pattern.compile("(\\n)|;"));` or Scanner.useDelimiter(Pattern.compile("[\\n;]"));` Edit : If \r\n is the problem, you might want to try this: Scanner.useDelimiter(Pattern.compile("[\\r\\n;]+")); which matches one or more of \r , \n , and ; . Note : I haven

Java Scanner question

我们两清 提交于 2019-12-28 13:38:10
问题 How do you set the delimiter for a scanner to either ; or new line? I tried: Scanner.useDelimiter(Pattern.compile("(\n)|;")); But it doesn't work. 回答1: As a general rule, in patterns, you need to double the \ . So, try Scanner.useDelimiter(Pattern.compile("(\\n)|;"));` or Scanner.useDelimiter(Pattern.compile("[\\n;]"));` Edit : If \r\n is the problem, you might want to try this: Scanner.useDelimiter(Pattern.compile("[\\r\\n;]+")); which matches one or more of \r , \n , and ; . Note : I haven

Scanner skipping every second line from file [duplicate]

你离开我真会死。 提交于 2019-12-28 02:18:07
问题 This question already has answers here : Scanner is skipping nextLine() after using next() or nextFoo()? (17 answers) How to use java.util.Scanner to correctly read user input from System.in and act on it? (1 answer) Closed last year . I'm trying to scan a text file and place each line into an arraylist, and stop scanning when the next line is a '*', however, my arraylist is storing every 2nd line and I am not sure why. Scanner scan = new Scanner(new File("src/p/input2.txt")); ArrayList

Is there an equivalent to the Scanner class in C# for strings?

与世无争的帅哥 提交于 2019-12-28 02:01:20
问题 In Java I can pass a Scanner a string and then I can do handy things like, scanner.hasNext() or scanner.nextInt() , scanner.nextDouble() etc. This allows some pretty clean code for parsing a string that contains rows of numbers. How is this done in C# land? If you had a string that say had: "0 0 1 22 39 0 0 1 2 33 33" In Java I would pass that to a scanner and do a while(scanner.hasNext()) myArray[i++] = scanner.nextInt(); Or something very similar. What is the C#' ish way to do this? 回答1: I

I have an issue using tokens, scanning multiple inputs and executing them at the same time

久未见 提交于 2019-12-25 19:37:09
问题 I am having an issue, i need to scan different inputs that the user will insert on the screen at the same time. For example i have a sort of a menu that is printed on the screen and the user will select if he wants to load a file and type the name of file.I have done this by asking the user to type load and after that asked him what file to load by I need it to be done at the same time. The user just types "load S.txt" and it selects the loads option and opens the file at the same time.My