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
๐ŸŒ
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 - It forms the base for performing calculations, conditions, and logical operations in Java programs ยท It explains different methods like Scanner and BufferedReader to take integer input from the user and display it on the screen.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Scanner.html
Scanner (Java Platform SE 8 )
July 21, 2026 - InputMismatchException - if the next token does not match the Integer regular expression, or is out of range ... 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.
๐ŸŒ
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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ read-integers-from-console-in-java
Read integers from console in Java
To read integers from console, use Scanner class. ... 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.
Find elsewhere
๐ŸŒ
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 to scan it as integers. import java.util.Scanner; public class TestSystemInput { public static void getFinalExamScore() { 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 = scanner.nextInt(); for (int i = 0; i < totalCourses; i++) { System.out.println("Enter score in course " + (i + 1) + " : "); finalScore = finalScore + scanner.nextInt(); } System.out.println("Your final Score = " + finalScore); scanner.close(); } public static void main(String[] args) { getFinalExamScore(); } } Get Your Final Exam Score!
๐ŸŒ
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 ... data type of interest. In Java, the Scanner class allows us to read in input as Double Integer or a Float using the methods nextInt() and nextDouble()....
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Scanner.html
Scanner (Java Platform SE 7 )
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range ... 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ scanner-class-in-java
Scanner Class in Java - GeeksforGeeks
Scanner is a predefined class in Java used to take input from the user. It is present in the java.util package and supports different types of input such as integers, strings, float values, and characters.
Published: May 27, 2026
๐ŸŒ
UW Computer Sciences
pages.cs.wisc.edu โ€บ ~hasti โ€บ cs368 โ€บ JavaTutorial โ€บ NOTES โ€บ JavaIO_Scanner.html
Java Input and Output (I/O)
September 3, 2008 - As you can see, the Scanner class contains a method named nextInt that returns the next input data available as an int value, that is, if the next input in the input stream is a valid integer format.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_scanner_nextint.asp
Java Scanner nextInt() Method
Java Examples Java Videos Java ... string: // Create a scanner object Scanner myObj = new Scanner("An int is a number between -2,147,483,648 and 2,147,483,647"); // Print the value of every int in the scanner while(myObj.hasNext()) ...
๐ŸŒ
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
The Scanner class is part of the ... to read different types of input. For example, to read an integer, you can use the nextInt() method:...
๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ java-scanner
Beginner's Guide To Java Scanner (With Code Examples) | Zero To Mastery
When reading numbers, Scanner provides methods to handle both integers and floating-point values.
๐ŸŒ
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
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ print an integer (entered by the user)
Java Program to Print an Integer (Entered by the User) | Vultr Docs
September 27, 2024 - Create an instance of Scanner. Prompt the user to enter an integer. Use the nextInt() method to read the integer. Print the integer entered by the user. ... import java.util.Scanner; public class IntegerPrinter { public static void main(String[] ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ scanner nextint() java
Scanner nextInt() Java - Scaler Topics
May 4, 2023 - The nextInt() method of the java.util.Scanner class is used to read, scan, or parse the next token of an input as int. It can scan input through Scanner class from String, InputStream, File, etc. The syntax of the nextInt() method of the Scanner class is: ... The nextInt() method converts the ...
๐ŸŒ
University of Texas
cs.utexas.edu โ€บ ~ndale โ€บ Scanner.html
Scanner class
Let's write a code fragment that instantiates a scanner and reads and prints an integer value and a second integer value if there is one. ... There are methods equivalent to these for each of the Java built-in types.