java.util.scanner

Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info? [duplicate]

馋奶兔 提交于 2019-11-26 04:23:53
问题 This question already has answers here : Scanner is skipping nextLine() after using next() or nextFoo()? (16 answers) Closed 3 years ago . I am writing a simple program that prompts a user to enter a number of students, then asks the user to enter each student\'s name and score in order to determine which student has the highest score. I have written the program code and it compiles. First line asks for a number of students and waits for input. The second line is supposed to ask for a student

Scanner method to get a char

别来无恙 提交于 2019-11-26 03:41:43
问题 What is the Scanner method to get a char returned by the keyboard in Java. like nextLine() for String , nextInt() for int , etc. 回答1: To get a char from a Scanner , you can use the findInLine method. Scanner sc = new Scanner("abc"); char ch = sc.findInLine(".").charAt(0); System.out.println(ch); // prints "a" System.out.println(sc.next()); // prints "bc" If you need a bunch of char from a Scanner , then it may be more convenient to (perhaps temporarily) change the delimiter to the empty

Why am I getting InputMismatchException?

♀尐吖头ヾ 提交于 2019-11-26 03:24:40
问题 So far I have this: public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num < min || num > max) { System.out.print(\"Invalid. Re-enter number: \"); num = reader.nextDouble(); } return num; } and this: public void askForMarks() { double marks[] = new double[student]; int index = 0; Scanner reader = new Scanner(System.in); while (index < student) { System.out.print(\"Please enter a mark (0..30): \"); marks

How do I keep a Scanner from throwing exceptions when the wrong type is entered?

依然范特西╮ 提交于 2019-11-26 02:57:52
问题 Here\'s some sample code: import java.util.Scanner; class In { public static void main (String[]arg) { Scanner in = new Scanner (System.in) ; System.out.println (\"how many are invading?\") ; int a = in.nextInt() ; System.out.println (a) ; } } If I run the program and give it an int like 4 , then everything goes fine. On the other hand, if I answer too many it doesn\'t laugh at my funny joke. Instead I get this(as expected): Exception in thread \"main\" java.util.InputMismatchException at

Why am I getting InputMismatchException?

☆樱花仙子☆ 提交于 2019-11-26 02:20:58
So far I have this: public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num < min || num > max) { System.out.print("Invalid. Re-enter number: "); num = reader.nextDouble(); } return num; } and this: public void askForMarks() { double marks[] = new double[student]; int index = 0; Scanner reader = new Scanner(System.in); while (index < student) { System.out.print("Please enter a mark (0..30): "); marks[index] = (double) checkValueWithin(0, 30); index++; } } When I test this, it can't take double number and I

NoSuchElementException with Java.Util.Scanner

早过忘川 提交于 2019-11-26 01:27:27
问题 I am very new to Java but am working through the book Java: How to program (9th ed.) and have reached an example where for the life of me I cannot figure out what the problem is. Here is a (slightly) augmented version of the source code example in the textbook: import java.util.Scanner; public class Addition { public static void main(String[] args) { // creates a scanner to obtain input from a command window Scanner input = new Scanner(System.in); int number1; // first number to add int

Scanner vs. StringTokenizer vs. String.Split

烂漫一生 提交于 2019-11-26 01:25:38
问题 I just learned about Java\'s Scanner class and now I\'m wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and String.Split only work on Strings, so why would I want to use the Scanner for a String? Is Scanner just intended to be one-stop-shopping for spliting? 回答1: They're essentially horses for courses. Scanner is designed for cases where you need to parse a string, pulling out data of different types. It's very flexible, but

How to use Scanner to accept only valid int as input

和自甴很熟 提交于 2019-11-26 01:24:01
问题 I\'m trying to make a small program more robust and I need some help with that. Scanner kb = new Scanner(System.in); int num1; int num2 = 0; System.out.print(\"Enter number 1: \"); num1 = kb.nextInt(); while(num2 < num1) { System.out.print(\"Enter number 2: \"); num2 = kb.nextInt(); } Number 2 has to be greater than number 1 Also I want the program to automatically check and ignore if the user enters a character instead of a number. Because right now when a user enters for example r instead

Scanner vs. BufferedReader

半世苍凉 提交于 2019-11-25 23:59:13
问题 As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader . I also know that the BufferedReader read files efficiently by using a buffer to avoid physical disk operations. My questions are: Does Scanner performs as well as BufferedReader ? Why would you choose Scanner over BufferedReader or vice versa? 回答1: Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and

java.util.NoSuchElementException: No line found

笑着哭i 提交于 2019-11-25 23:37:54
问题 I got an run time exception in my program while I am reading a file through a Scanner. java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Day1.ReadFile.read(ReadFile.java:49) at Day1.ParseTree.main(ParseTree.java:17) My code is: while((str=sc.nextLine())!=null){ i=0; if(str.equals(\"Locations\")) { size=4; t=3; str=sc.nextLine(); str=sc.nextLine(); } if(str.equals(\"Professions\")) { size=3; t=2; str=sc.nextLine(); str=sc.nextLine(); } if(str