Read an int from the command line
suggest changeimport java.util.Scanner;
Scanner s = new Scanner(System.in);
int number = s.nextInt();
If you want to read an int from the console, just use this snippet. First of all, you have to create a Scanner object that listens to System.in, which is standard input. After that, with the help of the Scanner object, you read the first int that the user types and store it in the variable number. Now you can do whatever you want with that stored int.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents