Java Reference
In-Depth Information
Fix this error, recompile the program, and try the three test cases again. You will now
get the output
Syllables in hello: 1
Syllables in yellow: 1
Syllables in peach.: 1
As you can see, there still is a problem. Erase all breakpoints and set a breakpoint in
the countSyllables method. Start the debugger and supply the input Ñhello.Ñ .
271
272
When the debugger stops at the breakpoint, start single stepping through the lines of
the method. Here is the code of the loop that counts the syllables:
boolean insideVowelGroup = false;
for (int i = 0; i <= end; i++)
{
ch = Character.toLowerCase(text.charAt(i));
if (ÐaeiouyÑ.indexOf(ch) >= 0)
{
// ch is a vowel
if (!insideVowelGroup)
{
// Start of new vowel group
count++;
insideVowelGroup = true;
}
}
}
In the first iteration through the loop, the debugger skips the if statement. That
makes sense, because the first letter, ÓhÓ , isn't a vowel. In the second iteration, the
debugger enters the if statement, as it should, because the second letter, ÓeÓ , is a
vowel. The insideVowelGroup variable is set to true , and the vowel counter is
incremented. In the third iteration, the if statement is again skipped, because the
letter ÓlÓ is not a vowel. But in the fifth iteration, something weird happens. The
letter ÓoÓ is a vowel, and the if statement is entered. But the second if statement is
skipped, and count is not incremented again.
Why? The insideVowelGroup variable is still true, even though the first vowel
group was finished when the consonant ÓlÓ was encountered. Reading a consonant
should set insideVowelGroup back to false . This is a more subtle logic error,
Search WWH ::




Custom Search