java.util.scanner

Multiple delimiters in Scanner class of Java

廉价感情. 提交于 2020-01-09 07:54:10
问题 How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\n) as delimiters? I am parsing some text from a csv file. 回答1: Scanner s = new Scanner("hello, world \n hello world"); s.useDelimiter(",|\\n"); while(s.hasNext()){ System.out.println(s.next()); } Output hello world hello world JavaDoc 回答2: How about useDelimiter(",|\\n"); 回答3: useDelimiter takes a regex pattern, so, it would be something like ",|\n" 回答4: Jigar is absolutely

Scanner only reads file name and nothing else

余生长醉 提交于 2020-01-08 18:04:37
问题 I'm trying to implement a rudimentary lexer. I'm stuck on the file parsing at the moment. public ArrayList<Token> ParseFile () { int lineIndex = 0; Scanner scanner = new Scanner(this.fileName); while (scanner.hasNextLine()) { lineIndex++; String line = scanner.nextLine(); if (line.equals("")) continue; String[] split = line.split("\\s"); for (String s : split) { if (s.equals("") || s.equals("\\s*") || s.equals("\t")) continue; Token token = new Token(s, lineIndex); parsedFile.add(token); } }

Scanner only reads file name and nothing else

梦想与她 提交于 2020-01-08 18:03:07
问题 I'm trying to implement a rudimentary lexer. I'm stuck on the file parsing at the moment. public ArrayList<Token> ParseFile () { int lineIndex = 0; Scanner scanner = new Scanner(this.fileName); while (scanner.hasNextLine()) { lineIndex++; String line = scanner.nextLine(); if (line.equals("")) continue; String[] split = line.split("\\s"); for (String s : split) { if (s.equals("") || s.equals("\\s*") || s.equals("\t")) continue; Token token = new Token(s, lineIndex); parsedFile.add(token); } }

How to set cursor at the beginning of the txt file? [closed]

こ雲淡風輕ζ 提交于 2020-01-07 05:05:14
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . Same as topic: How to set cursor at the beginning of the txt file in the java? (im using scanner class) 回答1: I think I interpreted the question wrong first. I guess you can use RandomAccessFile to do what you wish to do. Here is a simple snapshot of the code that would demonstrate

How to set cursor at the beginning of the txt file? [closed]

眉间皱痕 提交于 2020-01-07 05:05:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . Same as topic: How to set cursor at the beginning of the txt file in the java? (im using scanner class) 回答1: I think I interpreted the question wrong first. I guess you can use RandomAccessFile to do what you wish to do. Here is a simple snapshot of the code that would demonstrate

Reading data from keyboard to store in string array

走远了吗. 提交于 2020-01-05 12:59:04
问题 String s[]; System.out.println("Enter loop value"); int t = s.nextInt(); for(int i=0;i<t;i++) { str[i]=s.nextLine(); } while it reading it gives null pointer exception 回答1: What you need is something like this: Scanner s = new Scanner(System.in); System.out.println("Enter loop value"); int t = s.nextInt(); // read number of element s.nextLine(); // consume new line String str[] = new String[t]; for(int i=0;i<t;i++) { str[i]=s.nextLine(); } System.out.println(Arrays.toString(str)); Hope this

Reading data from keyboard to store in string array

烂漫一生 提交于 2020-01-05 12:58:03
问题 String s[]; System.out.println("Enter loop value"); int t = s.nextInt(); for(int i=0;i<t;i++) { str[i]=s.nextLine(); } while it reading it gives null pointer exception 回答1: What you need is something like this: Scanner s = new Scanner(System.in); System.out.println("Enter loop value"); int t = s.nextInt(); // read number of element s.nextLine(); // consume new line String str[] = new String[t]; for(int i=0;i<t;i++) { str[i]=s.nextLine(); } System.out.println(Arrays.toString(str)); Hope this

Scanner in JAVA outputs gibberish instead of Hebrew

末鹿安然 提交于 2020-01-05 12:08:48
问题 I have a problem with scanning Hebrew in Netbeans. I am trying to Scan the user's input and present in the Console, but it appears as Gibberish. My code is: public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please input:"); System.out.println("Your input is: " + input.next()); } and this is the Console: run: Please input: שלום Your input is: ���� BUILD SUCCESSFUL (total time: 3 seconds) The same exact code works perfectly on Eclipse. If I

Java: populating Scanner with default value on Scanner.nextLine();

浪子不回头ぞ 提交于 2020-01-05 08:20:52
问题 I am writing a java program that runs a loop and keeps asking the user for input. The program then does a bunch of things with the string, and asks for another string and repeats. The issue is that many strings are very similar, so i would like to populate the prompt with the input from the last time in the loop. For instance: If the user enters a value as follows: Enter the SKU Number: APE-6603/A ... Then the next time it asks for an SKU, it will wait till the user presses enter as normal,

Using a java.util.Scanner from within a method causes a runtime error

一笑奈何 提交于 2020-01-05 08:17:21
问题 Why would the following code give me a runtime error? It gives me a NoSuchElementException when the method is called the second time. It works fine if I delete the call to stdin.close() , but then Eclipse gives a warning that the resource is not closed. Code: import java.util.Scanner; public class st { public static void main(String[] args) { System.out.println("First call"); getInt("Enter first int (1-10): "); System.out.println("Second call"); getInt("Enter second int (1-10): "); } /** *