java.util.scanner

Java Scanner with regex delimiter

痴心易碎 提交于 2019-12-06 11:29:09
问题 Why does the following code return false? Scanner sc = new Scanner("-v "); sc.useDelimiter("-[a-zA-Z]\\s+"); System.out.println(sc.hasNext()); The weird thing is -[a-zA-Z]//s+ will return true. I also can't understand why this returns true: Scanner sc = new Scanner(" -v"); sc.useDelimiter("-[a-zA-Z]\\s+"); System.out.println(sc.hasNext()); 回答1: A scanner is used to break up a string into tokens. Delimiters are the separators between tokens. The delimiters are what aren't matched by the

Error while redirecting the input from file

一笑奈何 提交于 2019-12-06 11:16:50
Here's my code: import java.util.Scanner; class Graph{ boolean [][]array; int N; Graph (){ array = new boolean [1001][1001]; N=0; } void read_graph() { Scanner sc = new Scanner(System.in); N = sc.nextInt(); sc.nextLine(); String str; String []substr; for (int K=1; K<=N; K++){ str = sc.nextLine(); substr = str.split(" "); for (int I=0; I<substr.length; I++) System.out.println(substr[0]+" "+substr[I]); } } void query(){ Scanner sc = new Scanner(System.in); int P, Q; int counter = 0; boolean flag = true; while (flag){ counter++; P = sc.nextInt(); Q = sc.nextInt(); sc.nextLine(); if ( P == Q && P

Java: using hasNextInt with do-while loop, it ignores integer inputs at even times

爷,独闯天下 提交于 2019-12-06 09:25:25
I am trying to learn Java programming by myself and came across the course CS106A provided by Stanford. It's a great free online course. I watched several of the lecture videos and I enjoyed them so far. I am now trying to do the assignments and I have this problem I can't solve by myself. It is the number 5 of this assignment . Basically, it requires the learner to create a console program to get some integers input by the user and in response, showing the biggest and smallest number. The following code is what I have done and the problem is when I try to input integers, it will skip the even

How do I close a scanner in java?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 03:47:40
When ever I run the following code, private static String input(String Message){ Scanner input_scanner = new Scanner(System.in); System.out.print("\n" + Message); String string = input_scanner.nextLine(); input_scanner.close(); return string; } I get this error: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at main.learn.input(learn.java:25) at main.learn.main(learn.java:13) I figured out it was something to do with the line input_scanner.close(); but when I remove it, I get warnings saying: Resource leak: "input

java: Read text file and store the info in an array using scanner class

和自甴很熟 提交于 2019-12-05 21:26:51
I have a text file include Student Grades like: Kim $ 40 $ 45 Jack $ 35 $ 40 I'm trying to read this data from the text file and store the information into an array list using Scanner Class. Could any one guide me to write the code correctly? Code import java.io.*; import java.util.*; public class ReadStudentsGrade { public static void main(String[] args) throws IOException { ArrayList stuRec = new ArrayList(); File file = new File("c:\\StudentGrade.txt"); try { Scanner scanner = new Scanner(file).useDelimiter("$"); while (scanner.hasNextLine()) { String stuName = scanner.nextLine(); int

how to read a text file using scanner in Java?

走远了吗. 提交于 2019-12-05 19:41:21
问题 This is my code to read a text file. When I run this code, the output keeps saying "File not found.", which is the message of FileNotFoundException . I'm not sure what is the problem in this code. Apparently this is part of the java. For the whole java file, it requires the user to input something and will create a text file using the input as a name. After that the user should enter the name of the text file created before again (assume the user enters correctly) and then the program should

Scanner nextLine() occasionally skips input

对着背影说爱祢 提交于 2019-12-05 15:58:42
Here is my code Scanner keyboard = new Scanner(System.in); System.out.print("Last name: "); lastName = keyboard.nextLine(); System.out.print("First name: "); firstName = keyboard.nextLine(); System.out.print("Email address: "); emailAddress = keyboard.nextLine(); System.out.print("Username: "); username = keyboard.nextLine(); and it outputs this Last name: First name: Basically it skips letting me enter lastName and goes straight to the prompt for firstName . However, if I use keyboard.next() instead of keyboard.nextLine() , it works fine. Any ideas why? Let me guess -- you've got code not

how to catch blank input with scanner class in java

断了今生、忘了曾经 提交于 2019-12-05 13:22:10
I am using the scanner class to capture user input from the command line (strings only), as an alternative to my previous question . The following seems to work fine, except the blank lines are not caught as they should by the second conditional. For example when I press enter, this should be captured as a blank line and the second conditional should be true. However a new blank line is displayed on the console everytime, with the entire console "scrolling" upward if I keep hitting enter, rather than the logic in the conditional. Is there a proper way to catch a blank input from the command

Java Scanner does not wait for input

喜欢而已 提交于 2019-12-05 12:56:34
I have two blocks of code here. One scanner properly waits user input, and the other just blows right through it and calls nextInt() which returns a NoSuchElementException . Here is the block that works: public void startGame() { out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player."); Scanner scan = new Scanner(System.in); p = scan.nextInt(); if (p == 1) p1 = new DumbPlayer("ONE"); if (p == 2) p1 = new SmartPlayer("ONE"); else p1 = new HumanPlayer("ONE"); out.println("Player2: 1 for dumb player, 2 for smart player, 3 for human player."); p = scan.nextInt(); if (p ==

Getting input from user in Console without using Scanner

走远了吗. 提交于 2019-12-05 11:49:49
I would like to know about other ways of getting input from users using other classes like BufferedReader ,etc rather than using Scanner class. So, was there any other way of getting input from the user? If so, was it efficient than Scanner class? if you are using the Java SE6 or higher then you can make use of Console clas Console console = System.console(); if (console==null){ System.out.print("console not available "); }else { String line = console.readLine("Enter name :"); System.out.print("your name :"+line); } You can use System.in directly, like this: BufferedReader r = new