• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input on command line  RSS feed

 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick question, how do you take an input variable into the program from a command line. As from my other program question the command line is: c:\java Fexpand 3e900000

so 3e900000 is the input into the program (args).

I know that this is an easy thing to do but I have just never done it.

Any help would be appreciated.

Thanks.

Gabe
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the most basic way to do it is...
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if its an int?
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd handle that by giving your variable the proper type.

int varName
String varName
long varName

Or whatever else you are expecting. You can also practice by adding in checks against invalid values.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gabriel White:
what if its an int?


They will always be passed to you as strings.
It will be up to you to convert the value to whichever type your program expects.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the assignment says, "Do not use Strings when you can avoid it." But it also tells you to use a command line argument like this. As Ben points out, these will always be Strings -- you can't avoid that. So your first step in using args[0] should be in converting it to another type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!