import java.util.*;
public class help {
public static void main(String[] args) {
System.out.print(isAllVowels("ugh".toLowerCase()));
}
public static boolean isAllVowels(String Str) {
if( Str.length() == 0) {
return true;
}
int b = Str.length();
for( int a=0; a<b; a++) {
char c = Str.charAt(a);
if(c!='a' || c!='e' || c !='i' || c!='o' || c!='u') {
return true;
}
}
return false;
}
}
Hi there, I'm new to java and the char comparing element that returns true always does so. I was wondering why it always does so? Additinally IntelliJ tells me that a is always less than b. Why is that so? Thanks.
The problem isn't that you're using a ton of or statements.
You're confusing the meaning of and and or. and is exclusive, returning true if both conditions are satisfied. or is inclusive, returning true if either condition is satisfied.
Think about it like this: let's say you need to buy a new pair of shoes, but you're picky. The shoes must be blue and suede. A pair of blue Chuck Taylor All-Stars satisfies the first condition, but not the second. Will you buy the Chucks? False. Someone robs Graceland and steals a pair of Elvis Presley's shoes and offers them to you. The shoes are blue and suede. Will you buy them? True.
If you're a less picky person, you'll take blue shoes or suede shoes. Blue Chucks, brown suede loafers, tan suede slippers, or blue flip flops all meet one of two criteria, and will therefore be true.
Your code, in plain English, is asking "if the character is not a, e, i, o or u, return true." Even though u is a vowel, u is not a, not e, not i and not o, so it returns true.
Using a bunch of or just means if any one of those conditions is true, then return true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With