well .. you have String values of your input in your String args.
so, considering you calling methods in the same class as your main method, I would assume you have two possible solutions:
1. you create a couple of static class variables, in which you store the values you get from your command line args. Since these would be class variables, they would be accessible in your static methods.
2. you pass your variables as parameters to your methods, so they will automatically be accessible in these methods. I would suggest to take this approach.
stultuske
Industrious Poster
4,372 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
Pass the String args[1] as a parameter to the method, and parse it as int or double if needed. Alternatively, you could parse it as int or double if needed and then pass the result to the method. The latter is nice because it allows you to detect bad input early on and just bug out with an error message.
if (args[0].charAt(0) == 'a')
{
try {
intMethod{Integer.parseInt(args[1]);
}
catch (NumberFormatException nfe)
{
System.out.println("Expected integer, got "+args[1]);
}
}
and so forth...
Unfortunately, there's no way to test whether a String can be parsed as a number without trying it, so you're going to generate an exception on bad input. This is a reasonable way to handle that.
Now I don't know what happens after this, but you'll probably want to include some logic to continue correctly depending on whether the input was good and the method called or not - this is left as an exercise. :)
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Skill Endorsements: 3
Question Answered as of 1 Year Ago by
stultuske
and
jon.kiparsky