We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,661 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Main method parameter

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?

7
Contributors
6
Replies
3 Years
Discussion Span
1 Year Ago
Last Updated
8
Views
omeralper
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

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]);
sciwizeh
Posting Pro in Training
489 posts since Jun 2008
Reputation Points: 77
Solved Threads: 25
Skill Endorsements: 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++

Eng.Hamzawy
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

stanleymicheal
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0704 seconds using 2.67MB