File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes integer command line arguments? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "integer command line arguments?" Watch "integer command line arguments?" New topic

integer command line arguments?

Khair Bahir

Joined: Aug 01, 2011
Posts: 14

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

Joined: Oct 25, 2008
Posts: 2700

Parse the argument to an int.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Khair Bahir

Joined: Aug 01, 2011
Posts: 14
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

Joined: Oct 14, 2005
Posts: 19973

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40

You can write the method as:

I agree. Here's the link:
subject: integer command line arguments?
It's not a secret anymore!