Make simple calculations The problem is instead of doing what is stated in the if statements, does the else line like the if statement is false. However, when printed the variables i am working with, seems i have managed to get the desired expressions. I can't see a problem with the code either so if you experienced coders can please help me. Btw there is probably better ways to do this but I am new to java so no harsh comments please. Thank you.

import java.util.Scanner;

public class Exercise13 {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);

        System.out.println("Please enter a mathematical expression in the form '&lt;operand&gt; &lt;operator&gt; &lt;operand&gt;'.");

        String expression = read.nextLine();
        int spacePos1 = expression.indexOf(' ');
        int spacePos2 = expression.lastIndexOf(' ');

        String val1 = expression.substring(0, spacePos1);
        String val2 = expression.substring(spacePos2 + 1);
        String operator = expression.substring(spacePos1 + 1, spacePos2);

        int num1 = Integer.parseInt(val1);
        int num2 = Integer.parseInt(val2);

        if (operator == "+") 
            System.out.println("The answer is " + (num1 + num2));
        else if (operator == "-")
            System.out.println("The answer is " + (num1 - num2));
        else if (operator == "*")
            System.out.println("The answer is " + (num1 * num2));
        else if (operator == "/" &amp;&amp; num2 !=0)
            System.out.println("The answer is " + (num1 / num2));
        else System.out.println("The answer is " + (num1 % num2));

        System.out.println(num1);
        System.out.println(num2);
        System.out.println(operator);
    }
}
When comparing actual values use .equals instead of ==

Pay attention to the == java operator, it compares the value of two object references instead of the actual value.

For example your variables are stored in memory like this:

0x000 "string1" //stored in String operator
0x001 "string2" 
0x002 "string3"
0x003 "string1" //stored in String temp
0x004 "string2"
0x005 "string3"


Performing operator == temp translates to "is 0x000 equal to 0x003?", while performing operator.equals(temp) translates to "is 'string1' equal to 'string1' ?"

In other words, use == only if you want to check if two objects are exactly the same objects in the same location in memory