1

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());
    }
  }
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Steve durl
  • 11
  • 2
  • 1
    Please tab correctly, add spaces between each { and } (I don't think there's a coding style that doesn't support this - the real question is whether a newline is necessary to delimit blocks) – Nicholas Pipitone Oct 11 '18 at 21:41
  • What exactly is the problem? I don't understand. Do you have a problem making the `map` store the correct data? Or do you have a problem printing it. Try making your question much more precise, and just have your 10line attempt in combination with the exact input-output you want the code to do. If you really need help with all of the code you posted, split it into several questions. – Nicholas Pipitone Oct 11 '18 at 21:42
  • This code could be simplified quite a bit. For example, why not read the whole file into a CharBuffer, convert the CharBuffer into a String, then use String's split method to break it into different words? Nevertheless, if you want to continue to use your current algorithm, at least break it up into smaller methods so it's easier to digest. – Frecklefoot Oct 11 '18 at 21:47
  • `Map` is the answer, and then you just have to [iterate it to get the results](https://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-java-map?rq=1). A data structure and its presentation are different issues. However, if the assignment specifically requires using an array, then a `Map` cannot be used; then again `ArrayList` won't meet the requirement either. So please carefully define the real requirements. – KevinO Oct 11 '18 at 21:55

0 Answers0