HELP! How would I beable to use the "main" method to take in two arguments?

D

Dave

Yes, basically how would I beable to use the "main" method to take in two
arguments, both will be integers.
 
G

Gordon Beaton

Yes, basically how would I beable to use the "main" method to take
in two arguments, both will be integers.

Your arguments will be passed to main as Strings.

To convert them to integers, use Integer.parseInt() on each of the
arguments.

/gordon
 
J

Joe

Yes, basically how would I beable to use the "main" method to take in two
arguments, both will be integers.


You add parametrs to the command line invocation, like this:
java MyClass 1 2

Then, in you main method, parse the Strings like this:

public static void main(String[] argv) {
int a = Integer.parseInt(arg[0]); // need ot catch a
NumberFormatException
int b = Integer.parseInt(arg[1]);
}
 
R

Roedy Green

Yes, basically how would I beable to use the "main" method to take in two
arguments, both will be integers.

main only eats Strings.

So you if put two ints on the command line what you get is an
String[2].

Your problem now is to convert each String to an int for further
processing.

see http://mindprod.com/converter.html for how.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top