Simple computation program with strings...
Hi. I'm trying to make a simple calculator type program out of strings..
you can easily see what i'm trying to do by looking at the code:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sin = new Scanner(System.in);
String returnval = sin.nextLine();
System.out.println(math(returnval));
}
static double math(String compute)
{
String[] words = compute.split(" ");
double x = Integer.parseInt(words[1]);
double y = Integer.parseInt(words[2]);
if(words[0].equalsIgnoreCase("Add"));
{
return x+y;
}
if(words[0].equalsIgnoreCase("Subtract"));
{
return x-y;
}
if(words[0].equalsIgnoreCase("Multiply"));
{
return x*y;
}
if(words[0].equalsIgnoreCase("Divide"));
{
return x/y;
}
}
}
i want the console to ask the user to enter for example "Multiply 5 6" and then it gives 30. But i'm getting an error which says "Unreachable code" - once i make more than one if statement... i don't know why this is happening.. also if i typed garbage [space] 5 [space] 6 it will still multiply the numbers... i want it to multiply 2 numbers ONLY if i typed 'multiply\Multiply' at the beginning
Please help me try and fix the code, thank you
Re: Simple computation program with strings...
Hi alias22 (for the 22nd time?)
/end joke
Please see the announcements page for this forum, and use code tags when posting code. Instructions on the code tag are on the linked page.
The compiler in this case has found code that is never possible to reach. In your code:
if (words[0].equalsIgnoreCase("Add"))
;
There is a ; immediately after your if statement.
This says the if conditional has completed. It means the same thing as:
if (words[0].equalsIgnoreCase("Add")) {
}
So because you have:
if (words[0].equalsIgnoreCase("Add"))
;
return x + y;
You terminate the if, and return from the method, leaving all following if statements to never have a chance to be executed, no matter the results of your first if.
Note that this extra ; appears immediately after more than one of your if statements.
Good luck with corrections and let us know what you find out!
Re: Simple computation program with strings...
Oh...My...God I can't believe I made that mistake
lol!
i was looking at this for about 2 hours and i didn't spot that, God my eyesight deceives me
Code java:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sin = new Scanner(System.in);
String returnval = sin.nextLine();
System.out.println(math(returnval));
}
static double math(String compute)
{
String[] words = compute.split(" ");
double x = Integer.parseInt(words[1]);
double y = Integer.parseInt(words[2]);
if(words[0].equalsIgnoreCase("Add"))
{
return x+y;
}
if(words[0].equalsIgnoreCase("Subtract"))
{
return x-y;
}
if(words[0].equalsIgnoreCase("Multiply"))
{
return x*y;
}
if(words[0].equalsIgnoreCase("Divide"))
{
return x/y;
}
return 0;
}
}
the code is now looking like this and yes, it works
Thanks for helping me!
Re: Simple computation program with strings...
If you would use the javac command's -Xlint option, the compiler would give you this error for the way you coded the if statement:
Quote:
warning: [empty] empty statement after if