Use Scanner.hasNextInt():

Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input.

Here's a snippet to illustrate:

Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!sc.hasNextInt()) sc.next();
int num1 = sc.nextInt();
int num2;
System.out.print("Enter number 2: ");
do {
    while (!sc.hasNextInt()) sc.next();
    num2 = sc.nextInt();
} while (num2 < num1);
System.out.println(num1 + " " + num2);

You don't have to parseInt or worry about NumberFormatException. Note that since the hasNextXXX methods don't advance past any input, you may have to call next() if you want to skip past the "garbage", as shown above.

Related questions

  • How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
Answer from polygenelubricants on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2017 › 09 › java-program-to-read-integer-value-from-the-standard-input
Java Program to read integer value from the Standard Input
October 25, 2022 - import java.util.Scanner; public ... Scanner(System.in); System.out.print("Enter any number: "); // This method reads the number provided using keyboard int num = scan.nextInt(); // Closing Scanner after the use scan.close(); // Displaying the number System.out.println("The number entered ...
🌐
Baeldung
baeldung.com › home › java › java io › integer.parseint(scanner.nextline()) and scanner.nextint() in java
Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java | Baeldung
January 8, 2024 - The Scanner.nextLine() method reads the whole line as a string from the scanner. So, if we want the result to be an Integer, we must convert the string into Integer by ourselves, for example, using the Integer.parseInt() method.
Discussions

io - How to read integer value from the standard input in Java - Stack Overflow
What class can I use for reading an integer variable in Java? ... Save this answer. ... Show activity on this post. ... It can also tokenize input with regular expression, etc. The API has examples and there are many others in this site (e.g. How do I keep a scanner from throwing exceptions ... More on stackoverflow.com
🌐 stackoverflow.com
java - How do I convert scanner input into an integer? - Stack Overflow
In Java, how, if possible, can I convert numerical scanner input (such as 2 or 87) into an integer variable? What I'm using now yields the error message: Exception in thread "main" java.lang. More on stackoverflow.com
🌐 stackoverflow.com
scan.nextInt vs Integer.valueOf(scan.nextLine())
scan.nextInt has a problem that u cannot take string input after athat (idk the cause), its better to use Integer.valueOf(scan.nextLine()) More on reddit.com
🌐 r/learnjava
23
49
August 22, 2020
My Java code will not wait for user input when I ask the user for a new input

See The Scanner class and its caveats from our wiki.

It explains your problem and the solution to it.

More on reddit.com
🌐 r/javahelp
6
1
October 14, 2018
🌐
TutorialsPoint
tutorialspoint.com › read-integers-from-console-in-java
Read integers from console in Java
... Allow a use to add an integer using the nextInt() method. System.out.print( "Enter first integer: " ); int a = myInput.nextInt(); In the same way, take another input in a new variable.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-read-and-print-an-integer-value-in-java
How to Read and Print an Integer Value in Java - GeeksforGeeks
May 12, 2026 - Reading and printing an integer value in Java is a fundamental concept used to handle user input and display output. It allows programs to interact with users by accepting numeric data and showing results. Java provides built-in support through classes like Scanner and output methods such as System.out.println(), making input-output operations simple and efficient.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Scanner.html
Scanner (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. For example, this code allows a user to read a number from System.in:
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-read-a-long-integer-input-using-scanner-in-java-417653
How to read a long integer input using Scanner in Java | LabEx
While the nextInt() method of the Scanner class is suitable for reading standard-sized integers, it may not be able to handle very large integer values, such as those that exceed the range of the int data type in Java.
🌐
DEV Community
dev.to › sbu_05 › reading-an-string-after-an-integer-4jbp
Reading a String after an Integer - DEV Community
March 12, 2021 - When receiving input, in most ... class allows us to read in input as Double Integer or a Float using the methods nextInt() and nextDouble()....
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java Input Handling: Read Multiple Integers from One Line - Java Code Geeks
November 25, 2024 - This method continuously reads ... with a non-integer or EOF):"); // Create a list to store the integers List<Integer> inputs = new ArrayList<>(); // Read integers until no more input is available while (scanner.hasNext()) ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Scanner.html
Scanner (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. For example, this code allows a user to read a number from System.in:
🌐
Java2Blog
java2blog.com › take-integer-input-java
How to Take Integer Input in Java – Java2Blog
September 16, 2022 - For example: intis read using nextInt(), floatis read using nextFloat(), double is read using nextDouble(), and String is read using nextLine() etc. We need to import java.util.Scanner class to use Scanner. To read integer input from the user first need to create an object of Scanner class ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-User-Input-example-String-next-int-long-char
Java Scanner User Input Example
This tutorial on how to use the Java Scanner for user input will quickly show you how to import java.util, take String, int, double and char input, and teach you the difference between the ...
Published: May 15, 2026
🌐
CodeGym
codegym.cc › java blog › java io & nio › scanner nextint() method in java
Scanner NextInt() Method in Java
December 25, 2024 - This example uses the System input ... System.out.println("Get Your Final Exam Score!\n"); int finalScore = 0; int totalCourses = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter total Courses: "); totalCourses ...
🌐
CodeJava
codejava.net › java-se › file-io › java-scanner-tutorial-and-code-examples
Java Scanner Tutorial and Code Examples
July 29, 2019 - [Master REST API Development and ... daily Java coding. Basically, a Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace (blanks, tabs, and line terminators). The parsed tokens can be converted into primitive types and Strings using various next methods. Here’s the simplest example of using a Scanner to read an integer number from ...
🌐
Programiz
programiz.com › java-programming › scanner
Java Scanner (With Examples)
March 13, 2024 - In the first example, we have used the nextLine() method to read a string from the user. Unlike next(), the nextLine() method reads the entire line of input including spaces. The method is terminated when it encounters a next line character, \n. To learn more, visit Java Scanner skipping the nextLine(). Java scanner can also be used to read the big integer and big decimal numbers.
🌐
GeeksforGeeks
geeksforgeeks.org › java › scanner-nextint-method-in-java-with-examples
Scanner nextInt() method in Java with Examples - GeeksforGeeks
July 11, 2025 - InputMismatchException: if the ... public class GFG1 { public static void main(String[] argv) throws Exception { String s = "Gfg 9 + 6 = 12.0"; // create a new scanner // with the specified String Object Scanner scanner = ...
🌐
UW Computer Sciences
pages.cs.wisc.edu › ~hasti › cs368 › JavaTutorial › NOTES › JavaIO_Scanner.html
Java Input and Output (I/O)
September 3, 2008 - PlanetIO.java has examples of console based input and output and file based input and output, written by Beck Hasti. ConsoleIO_Scanner.java has additional examples of console based input and output, originally written by Jim Skrentny.
🌐
Intellipaat
intellipaat.com › home › blog › scanner class in java
Scanner Class in Java: Syntax, Methods, and Examples
September 8, 2026 - First, the Scanner object will read the entire line and split it into three tokens: “50”, “3.14”, and “true”. Then, it will read “50” as an integer, “3.14” as a decimal number, and “true” as a boolean value.