vendredi 7 juin 2019

I have problem with Java: bad operand types for binary operator "==" at Calculator If Else Java [duplicate]

This question is an exact duplicate of:

i have problem when i try for running my Calculator program using If Else, and the error have a notive like this :

Error:(27, 21) java: bad operand types for binary operator '==' first type: char second type: java.lang.String

This is my full code :

package com.tutorial; import java.util.*;

public class Main {

public static void main(String[] args) {
    // a operator b

    Scanner inputUser;
    float a,b, hasil;
    char operator;

------------- CALCULATOR -------------

    inputUser = new Scanner(System.in);

    System.out.print("a value = ");
    a = inputUser.nextFloat();
    System.out.print("operator value = ");
    operator = inputUser.next().charAt(0);
    System.out.print("b value = ");
    b = inputUser.nextFloat();

    System.out.println("input user : " + a + " " + operator + " " + b);


    if(operator == "+"){


        hasil = a + b;
        System.out.println("addition result = " + hasil);

    }else if(operator == "-"){


        hasil = a - b;
        System.out.println("reduction result = " + hasil);
    }else if(operator == "*"){


        hasil = a * b;
        System.out.println("multiplication result = " + hasil);
    }else if(operator == "/"){


        hasil = a/b;
        System.out.println("division result adalah = " + hasil);
    }else{
        System.out.println("operator not found");
    }
}

}

Please help me buddy :)

Aucun commentaire:

Enregistrer un commentaire