I am currently teaching myself Java to prepare for college. I would like to have some prior experience with the language before entering the class headfirst and becoming completely lost.
I'm making a program to perform very simplistic calculations. The problem I'm having is that my program should ask for input from the user three times. Twice for numbers and once for the operator. After the two numbers are entered, though, my program outputs "answer is 0.0" (the default value of double answer.)
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); double number1 = input.nextDouble(); System.out.print("Enter second number: "); double number2 = input.nextDouble(); System.out.print("Enter operator: "); String operator = input.nextLine(); double answer = 0; if(operator == "+") { answer = number1 + number2; } else if(operator == "-") { answer = number1 - number2; } else if(operator == "*") { answer = number1 * number2; } else if(operator == "/") { answer = number1 / number2; } System.out.println("answer is " + answer); } }