Compare an integer typed with each Enum value

Asked

Viewed 1,411 times

1

Hello.

I want to check if the option that the user typed corresponds to an option in the menu. I’m using Enum to show the options and the user type a corresponding number. But I can’t. Let’s see the Enum on the menu and the class I’m testing.

package dominio;  

public enum EnumMenuInicial {  

    CADASTRAR(1), PESQUISAR(2), EXCLUIR(3);  

    public final int OPCAO;  

    private EnumMenuInicial(int opcaoEscolhida) {  
        EnumMenuInicial.OPCAO = opcaoEscolhida;  
    }  

} 

import java.util.ArrayList;  
import java.util.Scanner;  
import dominio.EnumMenuInicial;  

public class Clinica {  

    private static Scanner entrada = new Scanner(System.in);  
    private static ArrayList<Cliente> clientes = new ArrayList<>();  

    public static void main(String args[]) {  
        Clinica.exibirMenuInicial();  
        int opcao = Clinica.readInt();  

}  

    private static boolean cadastrar() {  
        System.out.println("Informe o nome");  
        String nome = entrada.nextLine();  

        System.out.println("Informe CPF");  
        int cpf = Clinica.readInt();  

        System.out.println("Informe telefone");  
        int telefone = Clinica.readInt();  

        System.out.println("Informe o estado");  
        String estado = entrada.nextLine();  

        System.out.println("Informe a cidade");  
        String cidade = entrada.nextLine();  

        Cliente cliente = new Cliente(nome, cpf, telefone, estado, cidade);  

        boolean inserido = Clinica.clientes.add(cliente);  

        return inserido;  
    }  

//limpar buffer  
    private static int readInt() {  
        int numero = entrada.nextInt();  
        entrada.nextLine();  
        return numero;  
    }  

    private static void exibirMenuInicial() {  
        int i = 1;  

        System.out.println("Escolha uma opção:");  

        for (EnumMenuInicial opcao : EnumMenuInicial.values()) {  
            System.out.println(i + ": " + opcao.toString());  
            i++;  
        }  
    }  

    private static boolean validarOpcao(int opcaoEscolhida) {  
        int i = 0;  
        for (EnumMenuInicial opcao : EnumMenuInicial.values()) {  
            if (opcaoEscolhida == OPCAO) {  

    return true;            }  
        }  
}  

    return false;  
    }  

}  

On the line:

            if (opcaoEscolhida == OPCAO) {   

make a mistake:

OPCAO cannot be resolved to a variable Clinica.java /clinical/src line 61 Java Problem >

if I change to:

            if (opcaoEscolhida == EnumMenuInicial.OPCAO) {   

Makes the mistake:

Cannot make a Static Reference to the non-static field Enummenuinicial.OPCAO Clinical.java /clinical/src line 63 Java Problem >

How can I rectify this situation?

  • In if (opcaoEscolhida == OPCAO) the correct would be if (opcaoEscolhida == opcao.OPCAO).

  • @Caffé Thank you, corrected.

3 answers

1

An alternative would be to check all Enum options using an foreach:

public enum EnumMenuInicial {

    CADASTRAR(1), PESQUISAR(2), EXCLUIR(3);

    private int opcao;

    private EnumMenuInicial(int opcaoEscolhida) {
        opcao = opcaoEscolhida;
    }

    public int getOpcao() {
        return opcao;
    }

    public static EnumMenuInicial procurarOpcao(int id) {
        for(EnumMenuInicial e : values()) {
            if (e.getOpcao() == id)
                return e;
        }
        return null;
    }

}

So just check using this method procurarOpcao passing the user input:

EnumMenuInicial opcao = EnumMenuInicial.prucurarOpcao(Integer.parseInt("3"));
if (opcao != null) {
    //Opcao é válida...
} else {
    //Opcao inválida...
}

0

I corrected it. Where is:

        if (opcaoEscolhida == OPCAO) {  

Placed:

        if (opcaoEscolhida == opcao.OPCAO) {  

The code presented in doubt, now corrected, was:

package;

public Enum Enummenuinicial {

CADASTRAR(1), PESQUISAR(2), EXCLUIR(3);  

public final int OPCAO;  

private EnumMenuInicial(int opcaoEscolhida) {  
    EnumMenuInicial.OPCAO = opcaoEscolhida;  
}  

}

import java.util.Arraylist;
import java.util.Scanner;
import domain.Enummenuinicial;

public class Clinica {

private static Scanner entrada = new Scanner(System.in);  
private static ArrayList<Cliente> clientes = new ArrayList<>();  

public static void main(String args[]) {  
    Clinica.exibirMenuInicial();  
    int opcao = Clinica.readInt();  

}

private static boolean cadastrar() {  
    System.out.println("Informe o nome");  
    String nome = entrada.nextLine();  

    System.out.println("Informe CPF");  
    int cpf = Clinica.readInt();  

    System.out.println("Informe telefone");  
    int telefone = Clinica.readInt();  

    System.out.println("Informe o estado");  
    String estado = entrada.nextLine();  

    System.out.println("Informe a cidade");  
    String cidade = entrada.nextLine();  

    Cliente cliente = new Cliente(nome, cpf, telefone, estado, cidade);  

    boolean inserido = Clinica.clientes.add(cliente);  

    return inserido;  
}  

//clear buffer
private Static int readInt() {
int number = input.nextInt();
nextLine input.();
Return number;
}

private static void exibirMenuInicial() {  
    int i = 1;  

    System.out.println("Escolha uma opção:");  

    for (EnumMenuInicial opcao : EnumMenuInicial.values()) {  
        System.out.println(i + ": " + opcao.toString());  
        i++;  
    }  
}  

private static boolean validarOpcao(int opcaoEscolhida) {  
    int i = 0;  
    for (EnumMenuInicial opcao : EnumMenuInicial.values()) {  
        if (opcaoEscolhida == opcao.OPCAO) {  

return true;            }  
    }  

}

return false;  
}  

}

  • This here: Enummenuinicial.OPCAO = optionEscolhida; also weird. I don’t even know how it could be working. The right thing would just be OPTION = optionSchool; or this.OPCAO = option;, since OPTION is an instance variable and not a class variable.

0

  1. I suggest you change your enum, you are using the class reference to change a variable final what is not correct, use the thisfor this.
  2. Add methods to your enum to facilitate their work.

I suggest the following amendment, there are other solutions, but this can be applied.

Class Enum

public enum EnumMenuInicial {  

    CADASTRAR(1), PESQUISAR(2), EXCLUIR(3);  

    public final int OPCAO;  

    private EnumMenuInicial(int opcaoEscolhida) {  
        this.OPCAO = opcaoEscolhida;  
    }  

    public static EnumMenuInicial getOpcao(int opcao){
        switch (opcao) {
            case 1:
                return CADASTRAR;
           case 2:
                return PESQUISAR;
            case 3:
                return EXCLUIR;
            default:
                throw new IllegalArgumentException();
        }
    }

} 

Validate Option

private static boolean validarOpcao(int opcaoEscolhida) {
    try {
        EnumMenuInicial opcao = EnumMenuInicial.getOpcao(opcaoEscolhida);
        System.out.println("Opcao Escolhida: " + opcao);
        return true;
    } catch (IllegalArgumentException e) {
        System.err.println("Opcao Inválida: " + opcaoEscolhida);
        return false;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.