In the main method of a program we always use the code as a parameter: String[] args
What does it mean?
In the tutorial of java sun, there is a code following

public static void main(String[] args) {	
        //this program requires two arguments on the command line  
        if (args.length == 2) {
			//convert strings to numbers
            float a = (Float.valueOf(args[0]) ).floatValue();  
            float b = (Float.valueOf(args[1]) ).floatValue();
            //do some arithmetic
            System.out.println("a + b = " + (a + b) );
            System.out.println("a - b = " + (a - b) );
            System.out.println("a * b = " + (a * b) );
            System.out.println("a / b = " + (a / b) );
            System.out.println("a % b = " + (a % b) );
        } else {
           System.out.println("This program requires two command-line arguments.");
        }

and the output supposed to be like that
a + b = 91.7
a - b = -82.7
a * b = 392.4
a / b = 0.0516055
a % b = 4.5
However i can't get it. I got "This program requires two command-line arguments". Why?

Recommended Answers

All 6 Replies

public class Main
{
    public static void main(String[] args) {
        //this program requires two arguments on the command line
        if (args.length == 2) {
            //convert strings to numbers
            float a = (Float.valueOf(args[0]) ).floatValue();
            float b = (Float.valueOf(args[1]) ).floatValue();
            //do some arithmetic 
            System.out.println("a + b = " + (a + b) );
            System.out.println("a - b = " + (a - b) );
            System.out.println("a * b = " + (a * b) );
            System.out.println("a / b = " + (a / b) );
            System.out.println("a % b = " + (a % b) );
        } else {
            System.out.println("This program requires two command-line arguments.");
        }
    }
}

For the above program, type this at the command line:

javac Main.java

then this:

java Main 5 6

"5" is args[0]. "6" is args[1]. They are parameters passed to the main function from the command line. The program requires that you pass it two parameters because that's what it needs to do its job. In line 5, it checks to make sure you've passed it two parameters, and if you haven't, you get an error message. Thus the following will all result in an error message:

java Main
java Main 5
java Main 5 6 7

These lines pass main 0, 1, and 3 arguments. It needs 2. Replace the numbers 5 and 6 with any numbers you like, but you must supply the program two numbers for the program to work.

please use code tags

the last post is correct, but to answer your first question: String[] args means that it takes an array of Strings arrays are a few more pages into the sun tutorial, and i think strings are too.

off topic:
i don't know why the tutorial would use

float a = (Float.valueOf(args[0]) ).floatValue();
float b = (Float.valueOf(args[1]) ).floatValue();

it could be less hard to understand with these two:

float a = Float.parseFloat(args[0]) ;
float b = Float.parseFloat(args[1]);

but why they use String args[]
why String ? & why args ??
if i change it to any thing will effect on my project !
And why when i delete it there isn't main method? other wise in C++

some times i get confused with the codes of java what should i do

Start your own thread if you have problems. Don't hijack someone else's very dead thread.

Member Avatar for hfx642

"String" to handle anything that it receives as arguments.
Letters, numbers, symbols, anything you could type in. No restrictions.
"args" which stands for arguments.
You could call it "fred" if you like.

Actually, they are called "arguments" on the calling end. (You pass arguments.)
However, they are called "parameters" on the method end. (You received parameters.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.