for(int j=0 ; j<str.length() ; j++) {
if(char[j]==(a||e||i||o||u))
count++;
}
I know the result of (a||e||i||o||u)
is a Boolean so can't compare but how can we check for multiple character presence?
for(int j=0 ; j<str.length() ; j++) {
if(char[j]==(a||e||i||o||u))
count++;
}
I know the result of (a||e||i||o||u)
is a Boolean so can't compare but how can we check for multiple character presence?
This is not doing what you want. Please use a stack
switch
statement:Or, since I'm a regex enthusiast, here's an approach using regular expressions! :)
There was a mistake in this code fixed later on, thanks to user2980077If you use the classes you can try with regex or simple String
If I really need to work with a
char[]
array and not with aString
instance, I always use theCharacter
class and regular expressions. If you don't know what regular expressions are you should learn them because they are very useful when working with strings. Also, you can practice at Regexr.For your example I'd use this:
What the if statement does is basically it makes a new
String
instance containing the current character just to be able to use the methods from theString
class. The methodboolean matches(String regex)
checks whether your string satisfies the conditions given with theregex
argument.One more for Java 8:
One of the ways to do this in using a
List
ofCharacter
and in java9 could be :One more for the clever regex department: