This is similar to a few of the other answers, but with some tweaks and explanations of why the changes from your code were made. On a side note though, if regular expressions are an option for you, take a look at Adi's answer. He gives you another way to approach the problem, but since this is homework not sure if it is a viable solution. Anyway, with explanations (skip to the bottom for the final product):
Changes to what you declare to start with
int vowelCount = 0;
// All the Strings/StringBuilders can be final since we will never reinstantiate them
final String defParagraph = "This is an example sentence.";
// We'll just make a lowercase version here. That way we don't
// .toLowerCase() everytime though the loop, and for people that complain
// about trying to optimize - I think it's easier to read too.
final String lowerCaseVersion = defParagraph.toLowerCase();
// Declare the stringBuilder with the size of defParagraph. That is the
// maximum size the vowel-less text could be, and ensures the stringBuilder
// won't overflow it's initial capacity and have to waste time growing.
final StringBuilder newParagraph = new StringBuilder(defParagraph.length());
// Not using the vowel array. We will remove the loop
// and just || them together - see below
//char[] vowels = {'a', 'e', 'i', 'o', 'u'};
// You didn't need sbParagraph for anything you had (defParagraph works fine).
// Also, you could have just been a regular String since you never changed it.
//StringBuilder sbParagraph = new StringBuilder(defParagraph);
// Don't need vowParagraph since we aren't tracking the actual vowels, just the count
//StringBuilder vowParagraph = new StringBuilder("");
Changes to the actual loop
for (int i = 0; i < lowerCaseVersion.length(); i++) {
// grab the current character
char tempChar = lowerCaseVersion.charAt(i);
if ('a' == tempChar || 'e' == tempChar || 'i' == tempChar
|| 'o' == tempChar || 'u' == tempChar) {
// It matched one of the vowels, so count it
vowelCount ++;
} else {
// It didn't match a vowel, so add it the consonants stringBuilder
// Oh, and append a character from the original, not the lowerCaseVersion
newParagraph.append(defParagraph.charAt(i));
}
}
And then all together without the comments:
int vowelCount = 0;
final String defParagraph = "This is an example sentence.";
final String lowerCaseVersion = defParagraph.toLowerCase();
final StringBuilder newParagraph = new StringBuilder(defParagraph.length());
System.out.println("Characters: " + defParagraph.length());
for (int i = 0; i < lowerCaseVersion.length(); i++) {
char tempChar = lowerCaseVersion.charAt(i);
if ('a' == tempChar || 'e' == tempChar || 'i' == tempChar
|| 'o' == tempChar || 'u' == tempChar) {
vowelCount ++;
} else {
newParagraph.append(defParagraph.charAt(i));
}
}
System.out.println("\tVowel: " + vowelCount);
System.out.println("\tDefPara: " + defParagraph.toString());
System.out.println("\tNewPara: " + newParagraph.toString());
And the output looks like:
Characters: 28
Vowels: 9
DefPara: This is an example sentence.
NewPara: Ths s n xmpl sntnc.
Notes
- Using
final
is really optional in this case. I tend to use it where I can, but it's probably a personal preference. This Question has some discussion on when to use final
and might be worth checking out.