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 beif (opcaoEscolhida == opcao.OPCAO).– Caffé
@Caffé Thank you, corrected.
– André Nascimento