Adrian
Adrian

Reputation: 16725

Command line arguments not getting counted

I am working my way through "Java: A Beginner's Guide", Sixth Edition and I've encountered an area where I'm typing precisely what the book says, but I'm getting a undesired output.

Here's my class:

// Display all command-line information
public class CLDemo {

    public static void main(String args[]) {
        System.out.println("There are " + args.length + " command-line arguments");

        System.out.println("They are: ");
        for(int i = 0; i < args.length; i++)
            System.out.println("arg[" + i + "]: " + args[i]);
    }
}

My console output:

There are 0 command-line arguments
They are: 

Desired console output:

There are 3 command-line arguments
There are: 
arg[0]: one
arg[1]: two
arg[2]: three

I'm using Eclipse IDE for Java Developers

Version: Kepler Service Release 1 Build id: 20130919-0819

Any thoughts re: why the number of arguments from my code don't match the book's count of arguments would be greatly appreciated.

Update:

The solution to my issue turned out to be very simple. I had been running the sample projects in Eclipse by pressing the "Run" button without specifying arguments, as I've done for the prior 164 pages of the book without issue. The book instructed me to execute the program from the command line as follows:

java CLDemo one two three // where one two three are the arguments passed

Thank you to those who steered me to the solution.

Upvotes: 2

Views: 101

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 121998

It seems you are running the program from Eclipse where by default zero arguments got passed.

If you want to pass your arguments, you can do it through run -- >run configuration --> arguments tab.

Guide

Upvotes: 4

Related Questions