You can use java.util.Scanner (API):

import java.util.Scanner;

//...

Scanner in = new Scanner(System.in);
int num = in.nextInt();

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 when the wrong type is entered?).

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

java - How to use Scanner to accept only valid int as input - Stack Overflow
(java) ... You can use other data types like Double reference type. For intance: "scanner.hasNextDouble()" 2018-10-10T19:22:26.113Z+00:00 ... not knowing what the kb object is, I'd read a String and then trying Integer.parseInt() and if you don't catch an exception then it's a number, if you do, read a new one, maybe by setting num2 to Integer.MIN_VALUE and using the same type of logic in your example... 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
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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:
🌐
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()) ...
🌐
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()....
🌐
javathinking
javathinking.com › blog › convert-scanner-to-int-java
Converting Scanner Input to Integer in Java — javathinking.com
In this example, we create a Scanner object to read input from the console. We then prompt the user to enter an integer and use the nextInt() method to read the input as an integer.
🌐
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 ...
🌐
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:
🌐
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 ...
🌐
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 = ...
🌐
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.
🌐
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.