Error when comparing if: "java: bad operand types for binary operator"

2

I have a small problem in which I try to compare an array with an int option in an IF entered by the user, I get an error. I do not program everything in the Main and I use several classes and I communicate with each other.

I would like to make the user select the joke he wants and give him a score of 5 and thus be able to tag him in the array of the selected joke.

The error:

  

java: bad operand types for binary operator '==' first type:   com.company.Model.Chiste second type: int

This is the code of the ManagerChiste class

public class ManagerChistes {
public Chiste[] chistes = new Chiste[10];

public void valorarChiste(int opcion, int valor) {
    for (int i = 0; i < chistes.length; i++) {
        if (chistes[i] == (opcion - 1)) { -----> Error
            chistes[i].valor = valor;
            break;
        }
    }
}

This is the code of the ScreenValueChiste class

public class PantallaValorarChiste {
public void iniciar(ManagerUsuarios managerUsuarios, ManagerChistes managerChistes) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("---------------------------");
    System.out.println("Chistometro :: Valorar Chiste");
    System.out.println();
    for (int i = 0; i < managerChistes.chistes.length; i++) {
        if (managerChistes.chistes[i] != null){
            System.out.println("-----------------------------------------------------------");
            System.out.println("Numero chiste:" + (i+1));
            System.out.println("Titulo:     |\t" + managerChistes.chistes[i].titulo);
            System.out.println("-----------------------------------------------------------");
        }
    }

    boolean esValido = false;

    while(!esValido) {
        System.out.println("1) Valorar chiste");
        System.out.println("2) Volver a ver chistes");
        System.out.println("3) Volver");
        System.out.println("4) Log out");
        System.out.println("5) Salir");
        String option = scanner.nextLine();

        if ("1".equals(option)) {
            System.out.println("¿Que chiste quieres valorar? Selecciona un nuemro.");
            int opcion = scanner.nextInt();
            System.out.println("Que valoracion le quieres dar? (Sobre 5)");
            int valor = scanner.nextInt();

            managerChistes.valorarChiste(opcion, valor);

            System.out.println("Valoracion añadida");
            PantallaMenuApp pantallaMenuApp = new PantallaMenuApp();
            pantallaMenuApp.mostrar(managerUsuarios, managerChistes);
        } else if ("2".equals(option)) {
            for (int i = 0; i < managerChistes.chistes.length; i++) {
                if (managerChistes.chistes[i] != null){
                    System.out.println("-----------------------------------------------------------");
                    System.out.println("Numero chiste:" + (i+1));
                    System.out.println("Titulo:     |\t" + managerChistes.chistes[i].titulo);
                    System.out.println("Cuerpo:     |\t" + managerChistes.chistes[i].cuerpo);
                    System.out.println("-----------------------------------------------------------");
                }
            }
            esValido = false;
        } else if ("3".equals(option)) {
            PantallaMenuApp pantallaMenuApp = new PantallaMenuApp();
            pantallaMenuApp.mostrar(managerUsuarios, managerChistes);
            esValido = true;
        } else if ("4".equals(option)) {
            PantallaInicio pantallaInicio = new PantallaInicio();
            pantallaInicio.iniciar(managerUsuarios, managerChistes);
            esValido = true;
        } else if ("4".equals(option)) {
            System.out.println("Hasta luego");
            esValido = true;
        } else{
            System.out.println("Error opcion no valida");
            Thread.sleep(2000);
            esValido = false;
        }
    }
}

}

    
asked by KennedyRC 29.05.2018 в 22:38
source

1 answer

3

The error is that you are making a comparison of a Chiste object with an integer, which is incorrect.

   if (chistes[i] == (opcion - 1)) { -----> Error

The message says something like:

  

java: incorrect operand types for the binary operator "=="   first type: com.company.Model.Chiste second type: int

You must obtain the "value" of the object Chiste and perform the comparison between values of integer type:

 if (chistes[i].valor == (opcion - 1)) { 
    
answered by 29.05.2018 / 22:41
source