nextInt() reads the next whole number from your input

nextInt() is a method in Java's Scanner class that pulls the next integer (whole number) from whatever source you are reading — keyboard input, a file, a string, or a network stream. When you call nextInt(), the Scanner looks ahead in the input, finds the next complete number, and hands it to your program as an int data type.

The method stops reading as soon as it hits a character that is not part of a number. If the input is "42 hello", nextInt() returns 42 and leaves " hello" in the input stream for the next read. If the next thing in the input is not a number at all, nextInt() throws an exception and your program crashes unless you catch the error.

Key Takeaways

  • nextInt() reads one whole number from the input and returns it as an int, leaving everything after that number untouched.
  • The method skips whitespace (spaces, tabs, line breaks) automatically before looking for the number.
  • If the next input is not a valid integer, nextInt() throws a InputMismatchException and stops your program unless you handle the error.
  • Always check hasNextInt() before calling nextInt() to avoid crashes when input is missing or malformed.
  • nextInt() does not consume the newline character at the end of a line, which can cause problems if you call nextLine() when ready after.

How nextInt() reads from the input stream

When you create a Scanner object and call nextInt(), the Scanner maintains a position in the input. It starts from that position, skips any whitespace (spaces, tabs, newlines), and reads digits until it hits something that is not a digit. That number becomes the return value, and the Scanner's position moves to the character right after the number.

This matters because the next call to nextInt() or any other read method will start from that new position. If you read "42 99", the first nextInt() returns 42 and the Scanner points at the space before 99. The second nextInt() skips that space and returns 99.

If the input contains a sign, nextInt() handles it: "-42" and "+42" both work. If the number is too large for an int (larger than 2,147,483,647 or smaller than -2,147,483,648), nextInt() throws an exception.

What happens when nextInt() fails

If the next non-whitespace character is not a digit or a sign, nextInt() throws an InputMismatchException. Your program stops unless you catch the exception. For example, if the input is "hello 42" and you call nextInt(), it throws an exception because "h" is not a number.

The Scanner does not consume the bad input when an exception is thrown. If you catch the exception and try to read again, you will hit the same bad input and throw the same exception again. You must either skip the bad input or move on to a different input source.

The safest pattern is to check hasNextInt() before calling nextInt(). This method returns true if the next input is a valid integer and false otherwise. If hasNextInt() returns false, you can handle the situation — prompt the user again, skip the line, or exit gracefully — instead of crashing.

The newline problem after nextInt()

A common mistake is calling nextInt() and then calling nextLine() on the next line. nextInt() reads the number but leaves the newline character in the input stream. When you call nextLine() when ready after, it sees that newline and returns an empty string instead of reading the next line of text.

The fix is to call nextLine() once after nextInt() to consume the leftover newline, then call nextLine() again to read the actual next line. Alternatively, read everything as strings with nextLine() and parse the numbers yourself using Integer.parseInt().

nextInt() with a radix for different number bases

Scanner also has nextInt(int radix), which reads a number in a base other than 10. For example, nextInt(16) reads a hexadecimal number, nextInt(2) reads binary, and nextInt(8) reads octal. The method works the same way — it skips whitespace, reads digits valid in that base, and throws an exception if the input does not match.

If you call nextInt(16) on the input "FF", it returns 255. If you call it on "GG", it throws an exception because G is not a valid hex digit. The radix must be between 2 and 36.

Comparing nextInt() to other Scanner read methods

Scanner has several methods for reading different types of input. nextInt() reads whole numbers. nextDouble() reads decimal numbers. nextLine() reads an entire line as a string, including spaces. next() reads a single word (up to the next whitespace). Each method skips leading whitespace but behaves differently with what comes after.

If you need to read a number that might have a decimal point, use nextDouble() instead of nextInt(). If you need to read text that contains spaces, use nextLine(). If you need to read a single word, use next(). Mixing these methods in the wrong order is the source of most Scanner bugs.

Real example: reading numbers from the user

Here is a working pattern for reading integers safely from the keyboard:

Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); if (input.hasNextInt()) {   int number = input.nextInt();   System.out.println("You entered: " + number); } else {   System.out.println("That is not a number");   input.nextLine(); // consume the bad input }

This code checks hasNextInt() before calling nextInt(). If the check passes, it reads the number safely. If it fails, it prints an error and calls nextLine() to clear the bad input from the stream so the program can continue or ask again.

Frequently Asked Questions

Why does my program crash when I call nextInt()?

The input is not a valid integer. Use hasNextInt() to check before calling nextInt(), or wrap the call in a try-catch block to catch InputMismatchException. If you are reading from a file or network, make sure the data is actually there and in the format you expect.

How do I read multiple numbers on the same line?

Call nextInt() multiple times. Each call reads the next number and skips whitespace. If the input is "10 20 30", three calls to nextInt() return 10, 20, and 30 in order. The Scanner remembers where it left off.

What is the difference between nextInt() and Integer.parseInt()?

nextInt() reads from a Scanner and skips whitespace automatically. Integer.parseInt() converts a string you already have into an int. Use nextInt() when reading from input. Use Integer.parseInt() when you have a string and need to turn it into a number.

Can nextInt() read negative numbers?

Yes. nextInt() recognizes the minus sign and reads negative numbers correctly. The input "-42" returns the integer -42. The plus sign also works: "+42" returns 42.

Why does nextLine() return an empty string after nextInt()?

nextInt() leaves the newline character in the input stream. Call nextLine() once after nextInt() to consume it, then call nextLine() again to read the actual next line. Or read everything as strings and parse numbers yourself.