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