Java Reference
In-Depth Information
6.7 A Sample Debugging Session
To have a realistic example for running a debugger, we will study a Word class
whose primary purpose is to count the number of syllables in a word. The class uses
this rule for counting syllables:
Each group of adjacent vowels (a, e, i, o, u, y) counts as one syllable (for example, the
Ȓeaȓ in Ȓpeachȓ contributes one syllable, but the Ȓe . . . oȓ in Ȓyellowȓ counts as two
syllables). However, an Ȓeȓ at the end of a word doesn't count as a syllable. Each
word has at least one syllable, even if the previous rules give a count of 0.
Also, when you construct a word from a string, any characters at the beginning or end
of the string that aren't letters are stripped off. That is useful when you read the input
using the next method of the Scanner class. Input strings can still contain
quotation marks and punctuation marks, and we don't want them as part of the word.
Here is the source code. There are a couple of bugs in this class.
ch06/debugger/Word.java
1 public class Word
2 {
3 /**
4Constructs a word by removing leading and trailing non-
5letter characters, such as punctuation marks.
6 @param s the input string
7 */
8 public Word(String s)
9 {
10 int i = 0;
11 while (i < s.length() &&
!Character.isLetter(s.charAt(i)))
12 i++;
13 int j = s.length() - 1;
14 while (j > i &&
!Character.isLetter(s.charAt(j)))
15 j--;
16 text = s.substring(i, j);
17 }
18
267
268
Search WWH ::




Custom Search