This question has already been answered:

I would like to contact you with a problem with my java calculator. Here is the code itself:

import java.util.Scanner; public class keken2{ public static void main(String arr[]){ Scanner input1 = new Scanner(System.in); Scanner input2 = new Scanner(System.in); Scanner input3 = new Scanner(System.in); int result = 0; System.out.println("enter first number:"); int num1 = input1.nextInt(); System.out.println("enter second number:"); int num2 = input2.nextInt(); System.out.println("enter operation(+, -, *, /):"); String oper = input3.next(); if(oper == "+"){ result = num1 + num2; System.out.println("result is:" + result); }else if(oper == "-"){ result = num1 - num2; System.out.println("result is:" + result); }else if(oper == "*"){ result = num1 * num2; System.out.println("result is:" + result); }else if(oper == "/"){ result = num1 / num2; System.out.println("result is:" + result); }else{ System.out.println("You have entered a wrong operator"); } } } 

But instead of the expected result, after entering all the data, only "You have entered a wrong operator" appears. Tell me please, what's the problem?

Reported as a duplicate by Sergey Gornostaev java May 12 at 1:42 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    why three scanner? - dSH

1 answer 1

You make the wrong comparison. String comparisons must be performed using the equals method of the String class:

 str1.equals(str2); 

Read more here: https://javarush.ru/groups/posts/equals-java-sravnenie-strok

  • thanks, your advice helped - kekula
  • Then mark the answer as correct (click on the gray checkbox next to the answer) - Miron