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;
}
}
}
}