Following simple code shows how to pass command line arguments to to Java program:

public class CommandLineInput {
  public static void main (String[] args) {
    for (String argument: args) {
       System.out.println(argument);
    }
  }
}

Running the program

javac CommandLineInput.java
java CommandLine 30000 students

Output of this program

30000
students

Explanation All command line arguments are passed to a String array named args defined on line 2 of the code. Since all arguments are stored in an array in the order they were passed, you simply need to access the array to get the command line parameters and cast them to another type in necessary.

Taking input from command line interactively

In the previous example, we took command line parameter as input. In this example, the program would prompt users for input and then it would process their input. The following programs simply divides two numbers.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Division {
  public static void main(String[] args) {
    int divident = 0;
    int divisor = 0;
    double quotient = 0;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Divident (integer): ");
    try {
      divident = Integer.valueOf(br.readLine());
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    System.out.println("Divisor (integer): ");
    try {
      divisor = Integer.valueOf(br.readLine());
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    quotient = divident / divisor;
    System.out.println("Quotient: " + quotient);
  }
}

Explanation In the 11th line, input is reader by InputStreamReader and stored in BufferedReader. Both values are casted to integers.