+ 5
How can we get the an integer as input for operation in java?
5 odpowiedzi
+ 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());
+ 3
Here's the example:
https://code.sololearn.com/cl72OrlmoR6q/?ref=app
+ 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!
+ 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