Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 name and wait for input, then a third line should print ans ask for that student's score, and wait for input but after the second line prints, the third line is immediately called (2nd line does not wait for input) and then I get a runtime error when trying to enter the requested information after the third line.

How do I adjust the code so that the second line prints and waits for a string to be entered before printing the third line?

import java.util.Scanner;  public class HighestScore {     public static void main(String[] args) {         Scanner input = new Scanner(System.in);          System.out.print("Enter the number of students: ");         int numOfStudents = input.nextInt();          System.out.print("Enter a student's name: ");         String student1 = input.nextLine();          System.out.print("Enter that student's score: ");         int score1 = input.nextInt();          for (int i = 0; i <= numOfStudents - 1; i++) {              System.out.println("Enter a student's name: ");             String student = input.nextLine();              System.out.println("Enter that student's score: ");             int score = input.nextInt();              if (score > score1) {             student1 = student;             score1 = score;             }         }         System.out.println("Top student " +         student1 + "'s score is " + score1);     } } 
like image 791
user1011064 Avatar asked Oct 24 '11 14:10

user1011064


People also ask

Why does my Scanner nextLine skip?

Why is Scanner skipping nextLine() after use of other next functions? The nextLine() method of java. util. Scanner class advances this scanner past the current line and returns the input that was skipped.

Does Java Scanner wait for input?

Bookmark this question. Show activity on this post.

How do you use nextLine after next?

“using nextline after nextint” Code Answer's nextInt() method - it only reads the int value. So when you continue reading with input. nextLine() you receive the "\n" Enter key. So to skip this you have to add the input.

How do I get a Scanner to read the next line in Java?

nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.


2 Answers

That's why I don't didn't like using a Scanner, because of this behavior. (Once I understood what was happening, and felt comfortable with it, I like Scanner a lot).

What is happening is that the call to nextLine() first finishes the line where the user enters the number of students. Why? Because nextInt() reads only one int and does not finish the line.

So adding an extra readLine() statement would solve this problem.

System.out.print("Enter the number of students: "); int numOfStudents = input.nextInt();  // Skip the newline input.nextLine();  System.out.print("Enter a student's name: "); String student1 = input.nextLine(); 

As I already mentioned, I didn't like using Scanner. What I used to do was to use a BufferedReader. It's more work, but it's slightly more straightforward what is actually happening. Your application would look like this:

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));  System.out.println("Enter the number of students: "); int numOfStudents = Integer.parseInt(input.readLine());  String topStudent = null; int topScore = 0; for (int i = 0; i < numOfStudents; ++i) {     System.out.print("Enter the name of student " + (i + 1) + ": ");     String student = input.nextLine();      // Check if this student did better than the previous top student     if (score > topScore)     {          topScore = score;          topStudent = student;     } } 
like image 185
Martijn Courteaux Avatar answered Sep 27 '22 18:09

Martijn Courteaux


    System.out.print("Enter the number of students: ");     int numOfStudents = input.nextInt();     // Eat the new line     input.nextLine();     System.out.print("Enter a student's name: ");     String student1 = input.nextLine(); 
like image 26
Prince John Wesley Avatar answered Sep 27 '22 19:09

Prince John Wesley