Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting User input with Scanner

I am trying to have a scanner take input in a loop. Once the user wants to finish he can exit this loop. I have tried many different ways to do it but there is always some 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 code above, which also happens on different methods I tried, is that when the user types Y, the Scanner will skip the first input for first name,and jump to the surname. If the user types N the loop stops correctly. Someone can explain the reason this happens, and how to overcome using Scanner class?

p.s: Doing something like while(sc.nextLine().equals("Y")), will cause the loop to terminate before getting input from user after first run of the loop.

like image 804
Giannis Avatar asked Oct 21 '12 16:10

Giannis


2 Answers

This is because you are using Scanner#next method. And if you look at the documentation of that method, it returns the next token read.

So, when you read user input using next method, it does not read the newline at the end. Which is then read by the nextLine() inside the while loop. And thus, your firstName contains a newline without you knowing.

So, you should use nextLine() in your while rather than next().

Similar is the case with nextInt method. It also does not read the newline. So, you can read using readLine and convert it to int using Integer.parseInt. It can throw NumberFormatException if input value cannot be converted to int. So you need to handle it accordingly.

You can try the below code: -

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 one thing, if you enter a value that cannot be passed to Integer.parseInt you will get an exception, and that input will be skipped. For that case, you need to handle it by using while loop.

Or, if you don't want to do that exception handling: -

You can add an empty sc.nextLine() after sc.nextInt(), that will consume the newline left over, like this: -

    // 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]");
like image 118
Rohit Jain Avatar answered Sep 27 '22 17:09

Rohit Jain


  • Use equalsIgnoreCase(..) to prevent the case of Y or N being lower case and vice versa.
  • Don't use sc.next() rather sc.nextLine() to achieve what you want, because next() only reads up to the next space while nextLine() reads up to the next \n break.
  • Don't use nextInt() read all data as String and then convert because nextInt() is like next() thus is does not read up to the next \n.

Try it like this:

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]");
}
like image 39
David Kroukamp Avatar answered Sep 27 '22 18:09

David Kroukamp