Check several options without repeating [closed]

0

Imagine the following that I have to verify the following entries:

  • Girl be Blonde
  • Chica2 be Brunette
  • Be 18 years old
  • be from Madrid

I have the following variables

String chica;
String chica2;
String ciudad;
Integer edad;

And this line of code to check if she's blonde.

if ( chica.equalsIgnoreCase("rubia") {
    //verifica que es cierto..

How can I continue to verify the rest of the data? And if any of the following is incorrect, just tell me what is wrong?

That is, I could do this:

if (chica.equalsIgnoreCase("rubia")
    && ciudad.equals("Madrid") ...

But I need to go little by little checking one by one. And do not repeat any data. That is, if chica1 is blonde chica2 has to be a brunette.

But for example if chica1 is brunette then the girl2 can be blonde. The idea is that the hair colors are different, never the same color.

    
asked by Montse Mkd 09.04.2016 в 20:45
source

1 answer

2

First we validate that the value of girl and girl2 are different to continue and validate the other data.

//Ingresa si el valor de chica y chica2 son diferentes.
if (!chica.equalsIgnoreCase(chica2)) {
}

With the previous validation, we already verified that the value of girl and girl2 are different. Once you enter the previous code, you can verify the other data. For example: once you find the first wrong data, return it.

//Ingresa si el valor de chica y chica2 son diferentes.
if (!chica.equalsIgnoreCase(chica2)) {

} else {
    //Vamos a verificar cual fue el error
    String error = (chica.equalsIgnoreCase("Rubia"))?"chica2":"chica";
    return error;
}

With the previous validation, in case both values of girl and girl2 are the same they will go to the else, if girl is Blonde, I will return girl2 as an error or if girl is a brunette, I will return girl as an error (According to the inputs the value girl should be Blonde). Now we will verify the other entries.

//Entrara si el valor de chica y chica2 son diferentes
if (!chica.equalsIgnoreCase(chica2)) {
    //Si la ciudad es diferente a Madrid retornara "ciudad" como error
    if(!ciudad.equalsIgnoreCase("Madrid"))
        return "ciudad";
    //Si la edad es != 18 retornara "edad" como error
    else if(edad != 18)
        return "edad";
} else {
    //Vamos a verificar cual fue el error
    String error = (chica.equalsIgnoreCase("Rubia"))?"chica2":"chica";
    return error;
}

Ready. I returned the first error I found in the validations. For example, if the value of girl and girl2 are different and the city is not Madrid, it will return me as a city error. Now, you can create a function that returns a String to know which was the first error it found, or you can return an Array or a String that concatenates the errors that are found. Then the function that returns the first error it finds.

public String getPrimerError(String chica,String chica2,String ciudad, Integer edad){
    String error = "";
    //Entrara si el valor de chica y chica2 son diferentes
    if (!chica.equalsIgnoreCase(chica2)) {
        //Si la ciudad es diferente a Madrid retornara "ciudad" como error
        if(!ciudad.equalsIgnoreCase("Madrid"))
            return "ciudad";
        //Si la edad es != 18 retornara "edad" como error
        else if(edad != 18)
            return "edad";
    } else {
        //Vamos a verificar cual fue el error
        error = (chica.equalsIgnoreCase("Rubia"))?"chica2":"chica";
    }
    return error;
}

If the return of the previous function is "", it is because there was not an error. I hope this helps you.

    
answered by 09.04.2016 / 21:57
source