How to compare with equals a question with 2 possible answers

4

I'm doing an exercise in java and I have to compare the answer entered:

  

Do you want to continue? s / n:

You must contemplate both s and S

How do I do it? I can only do it with an answer

    
asked by Masterweed 14.07.2017 в 13:40
source

1 answer

5

You can solve this by using the equalsIgnoreCase () check, which compares the text string against an object ignoring uppercase and lowercase. It will return true if the compared strings are equivalent (ignoring uppercase and lowercase). Otherwise, it will return false.

Example:

if("s".equalsIgnoreCase(respuesta)){
     //gestionas si es "si"
}else{
     //gestionas si es "no"
}

Another alternative would be to use the toLowerCase () function as indicated by partner @ lois6b, which convert all characters in the string to lowercase.

Example:

if("s".equals(respuesta.toLowerCase())){
     //gestionas si es "si"
}else{
     //gestionas si es "no"
}
    
answered by 14.07.2017 / 13:50
source