Use Scanner and Scanner.nextInt() method to take only Integer as input from the user.

Scanner sc = new Scanner(System.in);
int anyNumber = sc.nextInt();

If the user gives an input which is not an integer, it will throw an InputMismatchException.

Answer from JRodDynamite on Stack Overflow
Discussions

java - reading int from console - Stack Overflow
How can I convert a String array into an int array in java? I am reading a stream of integer characters into a String array from the console, with BufferedReader br = new BufferedReader (new More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 7, 2011
java - How to read an int from the console in a beginner-friendly way? - Stack Overflow
I understand programming in general but I do not know the Java APIs. Most advice on the web on how to read an int from the console is complicated. It involves BufferedReader and InputStreamReader and such. This is completely incomprehensible to a beginner. I want to provide him with a function static int readIntFromConsole... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java User Input and Difference between readInt and nextInt? - Stack Overflow
You don't have a readInt method. No, you can't use nextInt there, because you also don't have a nextInt method. You'll need an instance of Scanner to use them. ... Please align your code, thank you. ... You need something like a scanner to read-in values from console. The code should look like that: import java... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to take console input in java using System.in.read().
๐Ÿ’ฅ 2026 New Year's Sale ๐Ÿ’ฅ Take 50% off your first 6 months! (new subscribers only, renews at regular price) ยท Join the Treehouse affiliate program and earn 25% recurring commission More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
2
May 8, 2017
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ console.readint() doesn't exist?
r/learnjava on Reddit: Console.readInt() doesn't exist?
June 27, 2016 -

I'm trying to recreate this textbook exercise but I'm having trouble reading integers off the console. I'm aware of the scanner class but I'm very insistent on making this Console.readInt() method work.

Java's official java.io.Console documentation doesn't list readInt as a method of the class. Why doesn't it?

๐ŸŒ
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.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ eroberts โ€บ jtf โ€บ javadoc โ€บ student โ€บ acm โ€บ io โ€บ IOConsole.html
IOConsole
The IOConsole class makes it easier to interact with the user using text-based input and output in the style of a traditional console. Given a IOConsole object, you can write output to that console using the print and println methods, just as you would for the standard output stream.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ io โ€บ datainputstream_readint.htm
Java - DataInputStream readInt() method
The Java DataInputStream readInt() method reads four input bytes and returns one integer value.
Find elsewhere
๐ŸŒ
QBasic on Your Computer
chortle.ccsu.edu โ€บ javaLessons โ€บ chap105 โ€บ ch105_03.html
readInt()
import java.io.*; class ReadInts { public static void main ( String[] args ) { String fileName = "intData.dat" ; long sum = 0; try { DataInputStream instr = new DataInputStream( new BufferedInputStream( new FileInputStream( fileName ) ) ); sum += instr.readInt(); sum += instr.readInt(); sum += instr.readInt(); sum += instr.readInt(); System.out.println( "The sum is: " + sum ); instr.close(); } catch ( IOException iox ) { System.out.println("Problem reading " + fileName ); } } }
๐ŸŒ
Ucc
cs.ucc.ie โ€บ ~gavin โ€บ cs1001 โ€บ Notes โ€บ chap86 โ€บ ch86_3.html
readInt()
import java.io.*; class ReadInts { public static void main ( String[] args ) { String fileName = "intData.dat" ; long sum = 0; try { DataInputStream instr = new DataInputStream( new BufferedInputStream( new FileInputStream( fileName ) ) ); sum += instr.readInt(); sum += instr.readInt(); sum += instr.readInt(); sum += instr.readInt(); System.out.println( "The sum is: " + sum ); instr.close(); } catch ( IOException iox ) { System.out.println("Problem reading " + fileName ); } } }
๐ŸŒ
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 class Demo { public static void main(String[] args) { /* This reads the input provided by user * using keyboard */ Scanner scan = new 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 by user: "+num); } }
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ io โ€บ objectinputstream_readint.htm
Java - ObjectInputStream readInt() method
The Java ObjectInputStream readInt() method reads a single int (32-bit integer) from the input stream. It is used when an integer value has been written using writeInt(int i). Following is the declaration for java.io.ObjectInputStream.readInt()
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ io โ€บ console_readline.htm
Java - Console readLine() method
The Java Console readLine() method reads a single line of text from the console. Following is the declaration for java.io.Console.readLine() method โˆ’ NA This method returns the string containing the line read from the console, not including ...
๐ŸŒ
CodeHS
codehs.com โ€บ glossary โ€บ term โ€บ 155
Glossary Term: readInt | CodeHS
Java method that lets us read in an integer input from the user. For example in this program: ``` int age = readInt("Enter your age: "); System.out.println("Your age is: " + age); ``` If the user inputs 16, the output will be: ``` Your age is 16 ```
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ read-java-console-input
How to Read Java Console Input | 3 Ways To Read Java Input - DataFlair
April 19, 2022 - Java Console Tutorial-how to read input from console in Java: Java Bufferedreader Class, Scanner Class in Java, Console Class 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 ... 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()....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ datainputstream-readint-method-in-java-with-examples
DataInputStream readInt() method in Java with Examples - GeeksforGeeks
June 5, 2020 - // Java program to illustrate // DataInputStream readInt() method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create integer array int[] buf = { 191, 225, 480, 763, 500 }; // Create file output stream FileOutputStream outputStream = new FileOutputStream("c:\\demo.txt"); // Create data output stream DataOutputStream dataOutputStr = new DataOutputStream(outputStream); for (int b : buf) { // Write integer value to // the dataOutputStream dataOutputStr.writeInt(b); } dataOutputStr.flush(); // Create file input stream FileInputStream inputStream = new FileInputStream("c:\\demo.txt"); // Create data input stream DataInputStream dataInputStr = new DataInputStream(inputStream); while (dataInputStr.available() > 0) { // Print integer values System.out.println( dataInputStr.readInt()); } } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java io โ€บ read and write user input in java
Read and Write User Input in Java | Baeldung
January 8, 2024 - In JDK 6 and later, we can use the Console class from java.io package to read from and write to the console.