This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Passign argument to a fucntion at command Line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passign argument to a fucntion at command Line" Watch "Passign argument to a fucntion at command Line" New topic
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: 477
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: 37907
    
  22
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passign argument to a fucntion at command Line
 
Similar Threads
main(String[]args)
OCPJP Basic
Threads
How do you prevent command prompt pop-up/close it after a /C Start command?
command line arguments