| Author |
Passign argument to a fucntion at command Line
|
Ritika Misra
Ranch Hand
Joined: Jun 17, 2009
Posts: 30
|
|
hello ranchers,
can any one help me to passs arguments to a fucntion at command line.
example:
any help will be appreciated....
|
Java Dev User
|
 |
Rok Ć telcer
Ranch Hand
Joined: Nov 03, 2009
Posts: 101
|
|
Hi,
Well, thats why Java's main method has array of Strings.
For example:
lets compile and run the code
#javac ReadCmdArgs.java
#java ReadCmdArgs foo1 foo2 foo3
And the output would be:
argument[0] = foo1
argument[1] = foo2
argument[2] = foo3
Regards,
Rok
|
SCJP, SCWCD
|
 |
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
In JAVA the common terminology of what you call "Functions" are known as "Methods".
You can try doing this:
Rok's example is a detailed one, because it handles the null value situation as well.
|
 |
shirish katti
Ranch Hand
Joined: Nov 09, 2009
Posts: 34
|
|
Give command as: java className 10 20 30 40
These are interpreted by compiler as :
args[0]=10
args[1]=20
args[2]=30
args[3]=40
Therefore
a.abc(10,20,30...)
These 10,20,30 are command line arguments..
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
shirish katti wrote:Give command as: java className 10 20 30 40
These are interpreted by compiler as :
args[0]=10 . . .
[pedantic mode]
They are interpreted by the JVM as . . . Not the compiler, which "knows" nothing about them.
args[0]="10" not 10. It is a String, hence the "".
[/pedantic mode]
|
 |
shirish katti
Ranch Hand
Joined: Nov 09, 2009
Posts: 34
|
|
yes campbell i was just giving a overview we need to parse them if we need to convert them to a integer as Integer.ParseInt(args[0])
I concentrated only on passing command line arguments..
|
 |
shirish katti
Ranch Hand
Joined: Nov 09, 2009
Posts: 34
|
|
Campbell:
[pedantic mode]
They are interpreted by the JVM as . . . Not the compiler, which "knows" nothing about them.
args[0]="10" not 10. It is a String, hence the "".
[/pedantic mode]
Obviously it is going to be interpreted as a string..
As the command line arguments are going to be array of strings..
|
 |
 |
|
|
subject: Passign argument to a fucntion at command Line
|
|
|