Method is running 3 times

-4

I've created this method for a validation when the user will type the option that appears on the screen:

private int menu;
private int menuImprimir;

public int getMenu() 
    {
        return menu;
    }
    public void ativarStatus() 
    {
        this.status = "Ativado";
    }

   public void menuImprimir() 
    {
        System.out.println("Deseja Imprimir os dados?" +
                "\n1 - Sim" + 
                "\n2 - Não");       
                this.menuImprimir = input.nextInt(); 

                while(this.menuImprimir < 1 || this.menuImprimir > 2) 
                {
                    System.out.println("\nErro! - Digite uma opção válida");

                    menuImprimir();
                }

                switch(this.menuImprimir) 
                {
                case 1:
                    imprimir();
                    break;
                case 2:
                    System.out.println("Obrigado! ... ");
                    break;
                default:
                    System.out.println("Opção inválida!\nDigite novamente: ");
                    break;
                }
    }

If the client types the option other than 0 and 1 it stays within the while asking him to enter the correct option. But if the client type 3 times the wrong option and in the fourth he type the correct one the system is printing System.out.println("Concluído com Sucesso!"); "3 times to later leave the method, even if I put this return ..

    
asked by anonymous 15.06.2018 / 06:37

1 answer

2

I'll guess what the getMenu method does:

  • write options
  • asks the user to enter a number
  • le returns the number you typed

So every time it is called, it will require a new digit. In this line while(getMenu() < 0 || getMenu() > 1) will ask for the digit for the first time and compare it with zero. If it is not smaller, it will ask for a second digit to compare with 1. And, within the loop, the method is called by the third instead. Surely not what you want to do ...

The response of the first call must be stored in a variable so that the content of the call is compared, that is, the method is called only once ... only being called again when a new value is required.

Example, very simplified ( using question code structure ):

int menu = getMenu();
while (menu < 0 || menu > 1) {
    System.out.println(MENSAGEM_ERRO);
    if (menu == 0) {
        ...
        return;
    }
    if (menu == 1) {
        ...
        return;
    }
    ...

    menu = getMenu();
}

A slightly better structure would be:

while (true) {
    int menu = getMenu();
    if (menu == 0) {
        ...
        return;
    }
    if (menu == 1) {
        ...
        return;
    }
    System.out.println(MENSAGEM_ERRO);
}

(a bit) More 'advanced':

int menu;
while ((menu = getMenu()) < 0 || menu > 1) {
    System.out.println(MENSAGEM_ERRO);
}
switch (menu) {
    case 0:
        ...
        break;
    case 1:
        ...
        break;
    default: throw IllegalArgumentException("Error menu: " + menu);
}

Another better option (IMHO): method getMenu tests the given value, only returning valid values ...

    
15.06.2018 / 13:39