Why does not the do-while function validate a response with equalsIgnoreCase?

1

I want to validate a response in java with a method, but something happens with the condition of the do-while since it is not able to get out of the loop.

    public static String siNo() {
    Scanner in =new Scanner(System.in);
    String respuesta;
    do {
        System.out.print(" > ");
        respuesta=in.next();
        System.out.println();
        System.out.println("Estoy detro del metodo  "+respuesta);
    } while (!respuesta.equalsIgnoreCase("S") || !respuesta.equalsIgnoreCase("N"));

    return respuesta;
}

}

    
asked by David Palanco 06.11.2017 в 16:15
source

3 answers

1

There are several things that have come together to make a tremendous problem.

The first factor is the same choice of do ... while , in which case the loop is exited when the condition is false .

Then, the evaluations upside down.

Solution 1: If you want to exit by writing a S or a N regardless of whether they are uppercase or lowercase

    Scanner in = new Scanner(System.in);

    String respuesta;
    boolean bolStatus = true;
    do {
        System.out.print(" > ");
        respuesta = in.next();
        System.out.println();
        System.out.println("Estoy detro del metodo  " + respuesta);
        bolStatus = (respuesta.equalsIgnoreCase("S") || respuesta.equalsIgnoreCase("N"));
        System.out.println(bolStatus);

    } while (!bolStatus);

    /*Cerrar scanner*/
    in.close();

Test:

debug:
 > e

Estoy detro del metodo  e
false
 > d

Estoy detro del metodo  d
false
 > s

Estoy detro del metodo  s
true
BUILD SUCCESSFUL (total time: 7 seconds)

Solution 2: if you want the opposite

You only remove the ! from the evaluation of while :

    Scanner in = new Scanner(System.in);

    String respuesta;
    boolean bolStatus = true;
    do {
        System.out.print(" > ");
        respuesta = in.next();
        System.out.println();
        System.out.println("Estoy detro del metodo  " + respuesta);
        bolStatus = (respuesta.equalsIgnoreCase("S") || respuesta.equalsIgnoreCase("N"));
        System.out.println(bolStatus);

    } while (bolStatus);

    /*Cerrar scanner*/
    in.close();

Test:

debug:
 > s

Estoy detro del metodo  s
true
 >  n

Estoy detro del metodo  n
true
 > N

Estoy detro del metodo  N
true
 > S

Estoy detro del metodo  S
true
 > P

Estoy detro del metodo  P
false
BUILD SUCCESSFUL (total time: 14 seconds)
    
answered by 06.11.2017 / 17:08
source
2

Let's analyze your sentence:

while (!respuesta.equalsIgnoreCase("S") || !respuesta.equalsIgnoreCase("N"));

let's see it in parts:

Here% of% are asking if answer is equal to !respuesta.equalsIgnoreCase("S") (let's avoid the topic of uppercase and lowercase). and you are transforming that response into the opposite. therefore, when I enter a S , this returns N , which as you are denying it, is falso , that is, we stay within the loop.

For the other party, verdadero , we have the same problem. If we enter something other than !respuesta.equalsIgnoreCase("N") , this returns N .

So it is a logical problem, you want to be inside while it is not fulfilled or it is n and s. therefore, you should do something like:

RESPUESTA != S AND RESPUESTA != N

and the task for the home, is to translate that (keep in mind that || it's OR and & amp; is AND

    
answered by 06.11.2017 в 16:39
2

Block do..while executes a group of statements while a condition is met.

The stop condition you have defined is !respuesta.equalsIgnoreCase("S") || !respuesta.equalsIgnoreCase("N") . Applying the Boole Algebra the expression is equivalent to:

!(respuesta.equalsIgnoreCase("S") && respuesta.equalsIgnoreCase("N"))

As it is impossible for the answer to have two different values at the same time, the condition will always be fulfilled and the loop will not end ... never.

If you want to stop when there is a response other than S or different% N , you can use the following condition:

do {
// ...
} while (!respuesta.equalsIgnoreCase("S") && !respuesta.equalsIgnoreCase("N"));
    
answered by 06.11.2017 в 16:40