Java Language Scanner Read an int from the command line

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


import java.util.Scanner;

Scanner s = new Scanner(;
int number = s.nextInt();

If you want to read an int from the command line, just use this snippet. First of all, you have to create a Scanner object, that listens to, which is by default the Command Line, when you start the program from the command line. After that, with the help of the Scanner object, you read the first int that the user passes into the command line and store it in the variable number. Now you can do whatever you want with that stored int.

Got any Java Language Question?