while loop for error correction

1

I'm doing a kind of menu for a calculator in java, with a switch I have to choose between 3 values (1, 2 and 3) and inside case 1, I make another switch to choose the submenus (a, b, c and d) ), my problem is that I need that if you enter a value that is not valid (A value other than a, b, cod) ask me again for a valid value by keyboard, in the first menu I got it with an if (If not the value is not 1 or 2 or 3) and a do-while (As long as the value is different from 1, 2 or 3 it asks for a value again per keyboard) But in the submenu I can not make it work in the same way. I attach a screenshot. Thanks in advance. link

            System.out.println("a. Suma");           
            System.out.println("b. Resta");           
            System.out.println("c. Producto");           
            System.out.println("d. División");

            opcion = keyboard.nextLine();   
            char b = opcion.charAt(0); 

            if ( b != 'a' || b != 'b' || b != 'c' || b != 'd' )

                do {

                    System.out.println("Introduce un valor valido");
                    opcion = keyboard.nextLine();   
                    b = opcion.charAt(0);
                } while ( b != 'a' || b != 'b' || b != 'c' || b != 'd' );

            switch (b) {

                case 'a':

I want that if I enter by letter the letter a, b, c or d, do not enter inside the if and the if loop, with this code write what you type on the keyboard the program enters inside the loop and never comes out. I just want to get inside the if and the loop if you write anything other than a, b, c and d.

    
asked by Alvaro Luque Jimenez 04.11.2017 в 10:39
source

2 answers

0

You must change all || (OR) by & & amp; (AND).

Basically, you want that if the user does not enter any of the allowed letters, show him the error message and make him write again.

The condition you need is: If the variable b is not equal to "a", and besides, it is not equal to "b", and besides, it is not equal to "c", and besides, it is not equal to "d" ... That is to say, that the variable has none of those 4 values.

Using OR you are saying that if the variable does not have any of those values, error. Therefore when the user types "a", although it is a correct option of your menu, it will enter the if because "a" is not "b", nor "c", nor "d". It's like trying to ask the user to enter a letter that is "a", "b", "c" and "d" at the same time, I do not know if I explain.

With AND you force it to enter if if ALL the conditions of the if are fulfilled.

With OR you make it enter the if if only one of the conditions of the if is fulfilled.

It would stay like this:

        if (b != 'a' && b != 'b' && b != 'c' && b != 'd')

        do {

            System.out.println("Introduce un valor valido");
            opcion = keyboard.nextLine();
            b = opcion.charAt(0);
        } while (b != 'a' && b != 'b' && b != 'c' && b != 'd');
    
answered by 04.11.2017 в 12:45
0

Another way to solve the problem may be to use the default in case statement so depending on the value of b you can run the code in the case or if it is any other go to default . However in this case you would have to put the case within a while and create a variable boolean to get out of that while .

    
answered by 04.11.2017 в 21:03