Basically the program is supposed to search through a text file and find the amount of occurrences of different words and store them in an array and then display the word and the amount of times to the user. I tried using the map class but that was not successful due to the fact that it won't display the results. So I was wondering if anyone knows any other ways I could fix this file:
public class WordStats {
public static void main(String[] args) {
File textFile = new File("words.txt");
FileReader in;
BufferedReader readFile;
char charInFile;
String newWord = "";
String map=new String();
String existingMap=new String();
int wordIndex, index = 0;
ArrayList wordList = new ArrayList();
/* navigating the file */
try{
in = new FileReader(textFile);
readFile = new BufferedReader(in);
do{
charInFile = (char)readFile.read();
if(charInFile >= 'a' && charInFile <= 'z'){
while(charInFile >= 'a' && charInFile <= 'z'){
newWord += charInFile;
charInFile = (char)readFile.read();
}//end of while
wordIndex = wordList.indexOf(map);
if(wordIndex > -1){
existingMap = (String) wordList.get(wordIndex);
//existingMap.addOccurrence();
wordList.set(wordIndex, existingMap);
} else {
index = 0;
}
if(wordList.size() > 0){
do{
existingMap = (String) wordList.get(index);
index += 1;
}while(existingMap.compareTo(map) <= 0 && index <
wordList.size());
wordList.add(index-1, map);
}else{
wordList.add(map);
}
}//end of if
newWord = "";
}while(charInFile != (char)-1);
System.out.println("Word\t\tOccurrences");
for(Object Word : wordList){
System.out.println(Word);
}
readFile.close();
in.close();
}catch(FileNotFoundException e){
System.out.println("file could not be found.");
System.err.println("FileNotFoundException: " + e.getMessage());
}catch(IOException e){
System.out.println("problem reading the file.");
System.err.println("IOException: " + e.getMessage());
}
}
}