Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

integer command line arguments?

 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to loop through a statement block x number of times, where the user inputs the integer x from the command line. However, the default command line argument is a string, so inputting "3" will cause the statement block to execute only once, as opposed to 3 times. Any ideas?



This is the construct I've been using. I want args.length to be equal to whatever integer I input from the command line.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parse the argument to an int.
 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Unfortunately, I'm not quite sure how to do this. Can you show me how to do this in the context of equating integer input to args.length? Or perhaps direct me to a place to learn more about how I could do it? Thanks.
 
Paul Clapham
Sheriff
Pie
Posts: 20775
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be a little more explicit...

Everything passed in from the command line is done as a string. How would it know what to expect otherwise?

You then need to 'convert' the input into whatever you need. If you know the second param should be an int, then you convert it to and int (and probably save it in some new variable) and use it how you need to.
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write the method as:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic