Getting user input with a scanner

I am trying to run the scanner in a loop. As soon as the user wants to finish, he can exit this cycle. I tried many different ways to do this, but there is always some kind of problem. This is the code:

private void inputEntries() { Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number = sc.nextInt(); Student student = new Student(name, surname, number); students.add(student); try { addToFile(student); } catch (Exception ex) { Logger.getLogger(TextReader.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Continue?[Y/N]"); } } 

The problem with the above code, which also happens with the different methods I tried, is that when the user enters Y, Scanner skip the first input for the name and go to the last name. If the user types N, the loop stops correctly. Can someone explain the reason for this, and how to overcome the use of the Scanner class?

ps: Doing something like while(sc.nextLine().equals("Y")) will end the loop before entering the user after the loop’s first run.

+7
source share
4 answers

This is because you are using the Scanner#next method. And if you look at the documentation of this method, it will return the next token.

So, when you read user input using the next method, it does not read newline at the end. Then it is read by nextLine() inside the while . So your firstName contains newline without your knowledge.

So you should use nextLine() in yours, not next() .

Similarly with nextInt . It also does not read a new line. That way, you can read use readLine and convert it to int using Integer.parseInt . It can throw a NumberFormatException if the input value cannot be converted to int . Therefore, you need to handle it accordingly.

You can try the code below: -

 Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number = 0; try { number = Integer.parseInt(sc.nextLine()); } catch (IllegalArgumentException e) { e.printStackTrace(); } System.out.println("Continue?[Y/N]"); } 

But note that if you enter a value that cannot be passed to Integer.parseInt , you will get an exception and this entry will be skipped. In this case, you need to process it using a while .

Or, if you do not want to perform this exception handling : -

You can add empty sc.nextLine() after sc.nextInt() , which will consume newline left, for example:

  // Left over part of your while loop String surname = sc.nextLine(); System.out.println("Enter number"); int number = sc.nextInt(); sc.nextLine(); // To consume the left over newline; System.out.println("Continue?[Y/N]"); 
+9
source
  • Use equalsIgnoreCase(..) to prevent the case when Y or N are lowercase letters and vice versa.
  • Do not use sc.next() rather sc.nextLine() to achieve what you want, because next() only reads to the next space, and nextLine() reads to the next break \n .
  • Do not use nextInt() to read all the data as a String , and then convert, because nextInt() as next() , therefore, does not read until the next \ n.

Try the following:

 Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number=0; try { number = Integer.parseInt(sc.nextLine());//read int as string using nextLine() and parse }catch(NumberFormatException nfe) { nfe.printStackTrace(); } System.out.println("Continue?[Y/N]"); } 
+4
source

you can use

 String abc = "abc"; String answer = "null"; while (abc.equals("abc") { if (answer.equals("n") { break; } while (answer.equals("y")) { //Put your other code here System.out.print("Continue? [y/n]") answer = input.nextLine(); if (answer.equals("n") { break; } } } 

This should stop the program when entering

 "n" 

You will also have to import and create a scanner as follows:

 //This is how you import the Scanner import java.util.Scanner; //This is how you create the Scanner Scanner input = new Scanner(System.in); /** *The name */ "input" /** *is used because of */ input.nextLine 
+1
source
 public void display() { int i, j; for (i = 1; i <= 5; i++) { for (j = i; j < 5; j++) { System.out.print(" "); } for (j = 1; j < (2 * i); j++) { System.out.print(i % 2); } for (j = 1; j < ((5 * 2) - (i * 2)); j++) { System.out.print(" "); } for (j = 1; j < (2 * i); j++) { System.out.print(j); } System.out.println(); } for (i = 4; i >= 1; i--) { for (j = i; j < 5; j++) { System.out.print(" "); } for (j = 1; j <= i; j++) { System.out.print(j); } for (j = i - 1; j >= 1; j--) { System.out.print(j); } for (j = 1; j < ((5 * 2) - (i * 2)); j++) { System.out.print(" "); } for (j = 1; j < (2 * i); j++) { System.out.print(j); } System.out.println(); } } 
+1
source

All Articles