+ 1

Why this is not working

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan= new Scanner(System.in); System.out.println("CHOOSE AN OPERATOR * + / -"); String o= scan.nextLine(); System.out.println("Type Any Number"); int n1= scan.nextInt(); System.out.println("Type The Second Number:" ); int n2= scan.nextInt(); if (o=="+") { System.out.println(n1+n2); } }

22nd Mar 2022, 1:08 PM
SHANU SRIVASTAVA
3 Answers
+ 2
Use (o.equals("+")) instead of (o=="+") edit: you have a brace } missing at end. add it.
22nd Mar 2022, 1:20 PM
Jayakrishna 🇼🇳
+ 2
Thank you i gotcha
22nd Mar 2022, 1:33 PM
SHANU SRIVASTAVA
0
In java, the operator == reference comparision while equal() method compares content of operands.. You're welcome..
22nd Mar 2022, 1:49 PM
Jayakrishna 🇼🇳