Java Reference
In-Depth Information
return lo; //key must be inserted in location lo
} //end binarySearch
public static void addToList(String item, WordInfo[] list, int p, int n) {
//sets list[p].word to item; sets list[p].freq to 1
//shifts list[n] down to list[p] to the right
for (int h = n; h >= p; h--) list[h + 1] = list[h];
list[p] = new WordInfo(item, 1);
} //end addToList
public static void printResults(PrintWriter out, WordInfo[] list, int n) {
out.printf("\nWords Frequency\n\n");
for (int h = 0; h < n; h++)
out.printf("%-20s %2d\n", list[h].word, list[h].freq);
} //end printResults
public static String getWord(FileReader in) throws IOException {
//returns the next word found
final int MaxLen = 255;
int c, n = 0;
char[] word = new char[MaxLen];
// read over non-letters
while (!Character.isLetter((char) (c = in.read())) && (c != -1)) ;
//empty while body
if (c == -1) return ""; //no letter found
word[n++] = (char) c;
while (Character.isLetter(c = in.read()))
if (n < MaxLen) word[n++] = (char) c;
return new String(word, 0, n);
} //end getWord
} //end class P2_4WordFrequency
class WordInfo {
String word;
int freq = 0;
WordInfo(String w, int f) {
word = w;
freq = f;
}
void incrFreq() {
freq++;
}
} //end class WordInfo
Suppose the file passage.txt contains the following data:
Search WWH ::




Custom Search