1,105,402 Community Members

Main method parameter

Member Avatar
omeralper
Newbie Poster
14 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
VernonDozier
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]
Featured
 
0
 
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.

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

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]);
Member Avatar
Eng.Hamzawy
Newbie Poster
1 post since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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++

Member Avatar
stanleymicheal
Newbie Poster
1 post since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 164 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
0
 

"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.)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article