I'm trying to use the switch method to count the number of vowels and number of consonants in a string. There is alot online for this problem but I must make sure characters like (,,$,&). numbers and spaces are not included as a consonants.
I have this atm.
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount ++;
break;
default:
consonantCount ++;
break;
}
How could I change the default to a specific case that only allows characters between a and z?
How could I change the default to a specific case that only allows characters between a and z?
The default
case matches everything that has not be matched by other cases, and you can't change that. Non-default cases only match a single compile time constant, and you can't change that either.
So the closest you can come with a switch
statement will be to use an if
statement within the default
case.
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
vowelCount++;
break;
default:
if (ch >= 'a' && ch <= 'z') {
consonantCount++;
}
break;
}
As @Oliver points out in his Answer, there are other (arguably better) ways to write the above without using a switch
statement. But I disagree with his characterization of if
in default
as "bad practice".
To my mind, the two most important things when talking about code quality are:
(I think we can all agree that these are necessary (if not sufficient ) for good code, but I acknowledge that I have simplified this, for the sake of argument.)
However, reasoning that is based on "how specific constructs are intended to be used" is (IMO) based on two false assumptions:
An assumption that the designers of Java did actually intend this... absent any documentary evidence of their intention.
An assumption that common usage patterns in Java cannot (or should not) evolve beyond the designers' original intentions.
For these reasons, I disagree with Oliver's blanket assertion that if
statements in a default
are "bad practice". It depends on correctness and readability / maintainability of the code in context .
A switch
statement is not going to solve your problem nicely.
Personally, I consider it bad practice to nest an if
statement inside the default
case of a switch
statement and is a sign that your pattern may need tweaking. For example
if(a == b || a == c || a == d)
could be written as
switch(a) {
case b:
case c:
case d:
// Code.
break;
}
Switch
and if
statements serve different purposes. If you need to check a condition on the variable you are switching in your default
case, then you probably should use an if statement
or add more case
s.
I suggest the following:
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
} else if(ch >= 'a' && ch <= 'z') { // If the character code is between 'a' and 'z', inclusive.
consonantCount++;
}
This all seems a lot cleaner if you ditch your switch.
private static final String VOWELS = "aeiou";
public void count(String in) {
int vowel = 0, consonant = 0;
for (char c : in.toCharArray()) {
if (VOWELS(c) > -1) vowel++;
else if (c >= 'a' && c <= 'z') consonant++;
}
System.out.printf("Vowels: %d Consonants: %d\n", vowel, consonant);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.