I'm trying to check if a vowel exists in a string, if found I use break; to break the loop and return a message, but it doesn't work.
String palabra="manzana";
String[] arrayPalabra=palabra.split("");
String[] vocales=new String[]{"a","e","i","o","u"};
var listVocals=Arrays.asList(vocales);
for (int i=0;i<arrayPalabra.length;i++) {
boolean existe = listVocals.contains(arrayPalabra[i]);
if(existe==true){
System.out.println("Existe vocal");
break;
}else{
System.out.println("NO Existe vocal");
break;
}
}
Since you only need to know if your word contains a vowel or not, you would need your loop to execute first and then evaluate the conditionals, since if you use the
break
in both conditionalsif | else
your loop breaks .// using break only when a vowel is returned in the word
You could initialize a
listVocals
as a list of typeString
. Also (logically), your conditionif
should be outside the loopfor
, otherwise it would return antrue
orfalse
for each character in the string that returns the.split()
, and what we need is just onetrue
orfalse
.