George B
Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008
|
|
Re: command-line argument
|
Posted: Feb 20, 2008 10:02 AM
|
|
The args you seek should be entered on the command line, so you don't need to prompt the user for them. The user should just put whatever numbers they want to sum on the command line, e.g.:
java Doubletest 1.1 2.22 3
Then they'll get passed into your program in the args array. Here's a trimmed-down version of your program that works:
public class Doubletest
{
public static void main(String args[])
{
double total = 0.0;
for (String s : args)
{
double d = Double.parseDouble(s);
total += d;
}
System.out.printf("The sum should appear here: %.2f\n", total);
}
}
When I run it as above I get the following:
> java Doubletest 1.1 2.22 3 The sum should appear here: 6.32
There were a few other mistakes in the original: - do the parseDouble on 's', not args[0] (the for loop causes s to get the value of each of the args in turn) - don't bother with the 'clueless' check -- you'll just sum up all the args regardless how many there are - you were missing a newline ("\n") in your printf statement - your indentation needs work :)
|
|