If you want all the newest features, then you should definitely get jdk6. I use 5, because there is nothing in 6 that i need.
You can use a Regex to do that:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexFinder {
public static void main(String args[]) {
try {
new RegexFinder();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
RegexFinder() throws IOException {
// this is the Regex pattern:
// any vowels: aeiou
// spaces: \\s
// punctioation : ?.,! (note: the ! must be escaped \\! because in Regex
// it means NOT. we are just looking for the ! char
Pattern pattern = Pattern.compile("[aeiou\\s?.,\\!]");
// edit: forgot that you needed it to read from command line
while (true) {
System.out.print("Enter the string to search: ");
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
Matcher m = pattern.matcher(in.readLine());
// all the counter variables
int a = 0, e = 0, i = 0, o = 0, u = 0, space = 0, punks = 0;
while (m.find()) {
// this switch looks trough each character matched by the regex,
// and
// evaluates it
switch ((int) m.group().charAt(0)) {
case 'a':
a++;
break;
case 'e':
e++;
break;
case 'i':
i++;
break;
case 'o':
o++;
break;
case 'u':
u++;
break;
case ' ':
space++;
break;
default:
// if it is not any of the above, but still found by the
// regex,
// it must be a punctioation mark
punks++;
}
}
System.out.println("# of a=" + a + "\n# of e=" + e + "\n# of i=" + i
+ "\n# of o=" + o + "\n# of u=" + u + "\n# of spaces=" + space
+ "\n# of punctuation marks=" + punks);
}
}
}