[Logo]
Forums Register Login
Command line argument passing
I try to build this if-else by passing a command line arguament to the variable gpa.but i get an error. can someone help me please with this problem. thank you


Did you notice anything about the error when it happened? I ask because "I get an error" isn't very helpful information. It tells us nothing about your problem.

So what you should do is, when the error occurs, look at it. Error messages are designed to provide information to help the programmer fix the error, so by ignoring the error message you are throwing away valuable information.

If you still can't solve the problem after doing that, then tell us what the error is. And don't just post a vague paraphrase of the error message, copy and paste the actual error message.
Please see your private messages for an important administrative matter.

/Dave
Yeah thank you for advice. : I tried to pass the argument to the variable using command line. i tried in various ways . but still this error occurs.. This is the error message occured when i try to compile the program.
E.java:3: ';' expected
double gpa(args[0]);
^
E.java:3: not a statement
double gpa(args[0]);
^
E.java:3: ';' expected
double gpa(args[0]);
^
3 errors
What is gpa?What are you doing? Is gpa a method or double Variable ??, put code tags and format you code.
 

MohamedSalim malik wrote:double gpa(args[0]);


I think you're trying to convert args[0] into a double. Well, this is not the way. You can't convert anything into a double by just adding wrapping it in parentheses directly after the variable name. The only way to give any variable a value is still with the assignment operator: You'll be able to find that something on the Javadoc page of java.lang.Double. Check for a static method that takes a String and returns a double.
I thought, maybe he's not come across the code tags yet, so I'll add them to the original post. Unfortunately, that simply shows awkward indentation. You should indent multiple if elses like thisYou should also spell better than I do
Thank you Rob Spoor .. yeah that the point where i did the mistake. i didnt convert the string value to a double. then i corrected the source code, and it did work.. :

class E{
public static void main(String args[]){
double gpa=Double.parseDouble(args[0]);
if(gpa>=3.6)
{
System.out.println("First class");
}

else if(3.59>=gpa && gpa>=3.4)
{
System.out.println("Upper Second class");
}

else if(3.49>=gpa && gpa>=3.00)
{
System.out.println("Lower second class");
}

else if(2.99>=gpa && gpa>=2.00)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}
}
}
You're welcome. For your information, the other primitive wrapper classes have similar methods: Integer.parseInt, Byte.parseByte, etc.
The command line argument passess a string value so you have to convert that into Double to proceed with calculation. Double.parseDouble method is used to do the above convertion.(Wrapper classess)

use following
double gpa=Double.parseDouble(args[0]);

Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 835 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 21, 2018 15:49:47.