How can we get the an integer as input for operation in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How can we get the an integer as input for operation in java?

10th May 2018, 12:23 PM
Samar Singhai
Samar Singhai - avatar
4 Answers
+ 2
If you are using Java 6, you can use the following oneliner to read an integer from console: int num = Integer.parseInt(System.console().readLine());
10th May 2018, 2:31 PM
Samar Singhai
Samar Singhai - avatar
8th Jan 2019, 11:51 AM
voja
voja - avatar
+ 2
Use the parseInt() method. Assign the scanner input to a string variable, then use said method to convert it to an int. Just make sure to include the right exception handling if the user decides to not type a number!
10th May 2018, 12:25 PM
apex137
apex137 - avatar
+ 2
very simple Use a scanner by first importing Java.util.Scanner before starting a class then create an object of scanner in the method you will need integer input by typing Scanner sc=new Scanner (System.in); Then supposing you want to store the input in a variable named inp, you give a print statement: System.out.println("enter input"); then you type int inp=sc.nextInt(); now you can use this variable inp in any formula like a normal variable
12th May 2018, 12:46 PM
Divyansh Dabral
Divyansh Dabral - avatar