3

I've encountered a rather odd problem with the Java Scanner getting user input. I made a practice program which first reads a double using nextDouble(), outputs some trivial text and then uses the same scanner object to get a string input using nextLine().

Here is the code :

import java.util.Scanner;

public class UsrInput {
    public static void main(String[] args) {

        //example with user double input
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a number: ");
        double input = reader.nextDouble();
        if(input % 2 == 0){
            System.out.println("The input was even");
        }else if(input % 2 == 1){
            System.out.println("The input was odd");
        }else{
            System.out.println("The input was not an integer");
        }

        //example with user string input
        System.out.println("Verify by typing the word 'FooBar': ");
        String input2 = reader.nextLine();
        System.out.println("The string equal 'FooBar': " + input2.equals("FooBar"));
     }      
 }

Now obviously my intention is to ask for a second input, and print if it's true or false that the string input2 equals 'FooBar'. However when I run this it skips the second input and immediately tells me it's not equal. However if I change reader.nextLine() to reader.next() it suddenly works.

It also works if I create a new Scanner instance and use reader2.nextLine()

So my question is why is my Scanner object not asking me for new input? If I print out the value of "input2" it's empty.

YCF_L
  • 49,027
  • 13
  • 75
  • 115
Dr. Hoenikker
  • 47
  • 1
  • 6
  • http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo?rq=1 – Yashar Panahi Apr 22 '17 at 10:18
  • That is indeed the same problem (I couldn't find it in search), but it still leaves me to wonder why the problem occurs with nextLine() after nextDouble() but not with next()? Does next() simlpy ignore the newline in the buffer> – Dr. Hoenikker Apr 22 '17 at 10:21

1 Answers1

3

You have to clear your Scanner so you can use reader.nextLine();, like this :

if (input % 2 == 0) {
    System.out.println("The input was even");
} else if (input % 2 == 1) {
    System.out.println("The input was odd");
} else {
    System.out.println("The input was not an integer");
}


reader.nextLine();//<<--------------Clear your Scanner so you can read the next input


//example with user string input
System.out.println("Verify by typing the word 'FooBar': ");
String input2 = reader.nextLine();
System.out.println("The string equal 'FooBar': " + input2.equals("FooBar"));

Edit

why does 'next()' ignore the \n still left in the scanner?

You will understand with this example here :

next()

public static void main(String[] args) {
    String str = "Hello World! Hello Java!";

    // create a new scanner with the specified String Object
    Scanner scanner = new Scanner(str);

    while(scanner.hasNext()){
        System.out.println( scanner.next());
    }
    scanner.close();
}

Output

Hello
World!
Hello
Java!

nextLine()

public static void main(String[] args) {
    String str = "Hello World!\nHello Java!";

    // create a new scanner with the specified String Object
    Scanner scanner = new Scanner(str);

    while(scanner.hasNext()){
        System.out.println( scanner.nextLine());
    }
    scanner.close();
}

Output

Hello World!
Hello Java!

So we can understand that next() read word by word so it does not use \n like nextLine()

YCF_L
  • 49,027
  • 13
  • 75
  • 115
  • Thanks, this does indeed solve my problem and it seems my question was already asked and answered elsewhere. It leaves me with one question: why does 'next()' ignore the \n still left in the scanner? – Dr. Hoenikker Apr 22 '17 at 10:24
  • @Dr.Hoenikker next read word by word separated by a space, not like nextLine() it use \n you can understand more in my example – YCF_L Apr 22 '17 at 10:57
  • Ok that makes perfect sense, next() just ignores spaces and newlines all the same, and in my example ignores the \n left from nextDouble() and waits for my input! – Dr. Hoenikker Apr 22 '17 at 11:33
  • @Dr.Hoenikker correct not just space it can be any delimiter ok read the link that i already share you will understand more – YCF_L Apr 22 '17 at 11:35