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.