Skip to content

August 7, 2014

Work with Command Line Arguments in Java

by admin

Next example shows how to work with arguments in Java. We will input 3 numbers from command line, we will convert those numbers to integer variables and we will print those numbers. Note that parameters in Java are read as strings.

package learn_java;

public class Learn_java {
    public static void main(String[] args) {
        int arg1, arg2, arg3;
        if (args.length == 3)
            arg1 = Integer.parseInt(args[0]);
            arg2 = Integer.parseInt(args[1]);
            arg3 = Integer.parseInt(args[2]);
            System.out.println("Argument 1 is: " + arg1);
            System.out.println("Argument 2 is: " + arg2);
            System.out.println("Argument 3 is: " + arg3);
Read more from Java Examples

Leave a Reply


Note: HTML is allowed. Your email address will never be published.

Subscribe to comments