Problem with detecting Math operation.
Hello,
I am having trouble testing to see what Mathematica operator the user has in-putted.
Here is the function (There are others so don't worry that this wouldn't actually run)
Code :
public static void calc()
{
Scanner calcinputS = new Scanner(System.in);
String calcinput, op;
int answer, fnum, snum;
System.out.println("-------------------------");
System.out.println("| Calculator v1.0 |");
System.out.println("-------------------------");
System.out.print(" Enter an equation: ");
calcinput = calcinputS.nextLine();
String[] calcinputA = calcinput.split(" ");
fnum = Integer.parseInt(calcinputA[0]);
op = calcinputA[1];
snum = Integer.parseInt(calcinputA[2]);
if (op == "+")
{
answer = fnum + snum;
System.out.println(answer);
}
else if (op == "-")
{
answer = fnum - snum;
System.out.println(answer);
}
else if (op == "*")
{
answer = fnum * snum;
System.out.println(answer);
}
else if (op == "/")
{
answer = fnum / snum;
System.out.println(answer);
}
else
{
System.out.println("Error: Could not recognize operation: " + op);
}
}
Thank you
Re: Problem with detecting Math operation.
You should use the equals() method for comparing Strings, not the == operator.
Re: Problem with detecting Math operation.