This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passign argument to a fucntion at command Line

 
Ritika Misra
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers,

can any one help me to passs arguments to a fucntion at command line.


example:




any help will be appreciated....
 
Rok Ć telcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Somnath Mallick
Ranch Hand
Posts: 482
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic