English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have a question " write an application that uses an enhanced for statment to sum the double values passed by the command-line argumants .[hint:Use the static method parseDouble of class double to convert a String to a double vale]"
this is what i wrote but its wrong can u please help me ASAP----->


public class args{
public static void parsedouble(){

for (int i=0;i Double.parseDouble(args[i]);

}
}
public static void main(String args[]){
double total=0.0;

for(int i:args.length){
total+=parsedouble();
}
System.out.print(total);
}
}

2007-12-11 05:42:48 · 3 answers · asked by funky !! 2 in Computers & Internet Programming & Design

3 answers

Several things were wrong here. Below is how it should be. Also, you cannot name main() function parameter 'args' because your class already has that name.

public class args{
public static double parsedouble(String s){
return Double.parseDouble(s);
}
public static void main(String[] argz){
double total=0.0;
for(int i=0;i total+=parsedouble(argz[i]);
}
System.out.print(total);
}
}

2007-12-11 05:55:45 · answer #1 · answered by General Cucombre 6 · 0 0

You're not doing anything with the return value from Double.parseDouble(args[i]), and your parsedouble() method isn't returning anything.

Try total += Double.parseDouble(args[i]);

2007-12-11 05:56:50 · answer #2 · answered by daa 7 · 0 0

you do not even need your parsedouble(), just use this for loop in place of yours:

for(int i:args){
total+=Double.parseDouble(i);
}

2007-12-11 05:54:17 · answer #3 · answered by Nick B 3 · 0 0

fedest.com, questions and answers