EDIT: I changed my previous code where I compared strings with !=, to .equals(), but it's still the same.
I'm trying to make a beginner calculator. I'm stuck in this infinite loop which I can't figure out.
First I tried this:
public void GetValues()
{
System.out.println("Welcome to the \"Math Calculator\"\n");
System.out.print("Type in the first number: ");
int FirstNumber = Scan.nextInt();
System.out.print("Type in the second number: ");
int SecondNumber = Scan.nextInt();
System.out.println("\nWhat operation would you like to do?\n");
System.out.println("For addition, type \"+\"");
System.out.println("For subtraction, type \"-\"");
System.out.println("For multiplication, type \"*\"");
System.out.println("For division, type \"/\"\n");
MathOperation = Scan.next();
while (MathOperation != "+" || MathOperation != "-" || MathOperation != "*" || MathOperation != "/")
{
System.out.print("The value you typed, is not valid. Type again: ");
MathOperation = Scan.next();
}
}
Still, no matter what I type in, I still get this message The value you typed, is not valid. Type again:
I just can't figure it out. What am I missing?
EDIT2: I changed the loop into this:
while (!IsEqual)
{
System.out.print("The value you typed, is not valid. Type again: ");
MathOperator = Scan.next();
if (MathOperator.equals("+") || MathOperator.equals("-") || MathOperator.equals("*") || MathOperator.equals("/"))
{
IsEqual = true;
}
}
And now it works. Thanks to all who put effort on helping me. Cheers :)