Reading Input from User

Section 1: How to Accept Input from command line arguments?

Reading Input from User

We have written our first java program. Now let’s see how to pass input in the java main method and read the same in the code. Using command-line arguments, using a Scanner class, and using BufferedReader class.


Command-line arguments are argument values that are passed while running the java program. These arguments passed from the cmd console we can receive as an input in the java program and use inside the code.

In the following program, we accept one parameter from the command line and show it into the output. Since we are passing only one parameter we can read it at the 0’th position of the array as shown in the program.

If we want to pass multiple values from the command line, just separate them by space, if our value itself contains space in it then pass it within double-quotes like “Java Program”.
Let’s see the following example for passing and reading multiple values in java.

If we want to pass any other data type value like Integer, we should explicitly parse the string value into integer using Integer.parseInt(args[0]).
So we know how to read arguments passed from the command line, next we will see how to read arguments using the Scanner class.

-A blog by Shwetali Khambe

Related Posts