DEV Community

Yuya Hirano
Yuya Hirano

Posted on

How behave 'Scanner', 'nextInt()', and 'nextLine()'?

Oneday, I programed system worked weird.

When I tried to have the user enter a string after entering a numerical value, the system terminated after entering the numerical value.
In other words, the string input step was skipped.
I don't expected behave this.

public class App {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("What is number1?");
        int number1 = scan.nextInt();

        System.out.println("What is string1?");
        String string1 = scan.nextLine();

        System.out.println("number1 is " + number1);

        System.out.println("string1 is " + string1);
    }
}
Enter fullscreen mode Exit fullscreen mode

As a result of the above syntax.

number1 is 1
string1 is 
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

nextLine()

public int nextInt(int radix)
Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.

so what?

Let's run some verification programs.

Scanner scan = new Scanner(System.in);

int num1 = scan.nextInt();

System.out.println("What is number?");
System.out.println(num1);

System.out.println("What is string?");
String str = scan.nextLine();

System.out.println(str);
System.out.println("end");
Enter fullscreen mode Exit fullscreen mode
1

end
Enter fullscreen mode Exit fullscreen mode

In other words, if you enter a number in the first code and press Enter, "number + \n" will be entered into the "scan".
When this is read by "nextline()", only the number is read.
Then "\n" will remain in "scan".
I thought I would enter the value in the next step, but "scan" already contains "\n".
This is the same behavior as the enter key being pressed.

How to resolve.

You can use "next()".

public class App {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("What is number1?");
        int number1 = scan.nextInt();

        System.out.println("What is string1?");
        String string1 = scan.next(); //fixed

        System.out.println("number1 is " + number1);

        System.out.println("string1 is " + string1);
    }
}
Enter fullscreen mode Exit fullscreen mode
What is number1?
1
What is string1?
str
number1 is 1
string1 is str
Enter fullscreen mode Exit fullscreen mode

and also, using "parseInt" You can fixed.

public class App {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("What is number1?");
        int number1 = Integer.parseInt(scan.nextLine()); //fixed

        System.out.println("What is string1?");
        String string1 = scan.nextLine();

        System.out.println("number1 is " + number1);

        System.out.println("string1 is " + string1);
    }
}
Enter fullscreen mode Exit fullscreen mode
What is number1?
1
What is string1?
str
number1 is 1
string1 is str
Enter fullscreen mode Exit fullscreen mode

thank you.

Top comments (0)