1

How would you find the most occurring words in a file that has five or more letters using an input/output program? This is a starter code that i have

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class FileIOtest {

/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File file = new File ("myfile.txt");
    Scanner inputFile = new Scanner(file);

    while(inputFile.hasNext())
    {
        String str =inputFile.nextLine();
        System.out.println(str);
    }
    inputFile.close();

}

}
user3012019
  • 75
  • 1
  • 1
  • 3

1 Answers1

0

I would make a hashmap that holds a key-value pair between a word and a counter for the number of occurrences.

Map<String, Integer> myMap = new HashMap<String, Integer>();

You should split each line by white-space then iterate through the array of the split string. You can then check if the word is 5 or more characters and increment the counter in your hashmap

    String str = inputFile.nextLine();
    String[] parts = str.split(" ");
    for(int x  = 0; x < parts.length; x++)
    {
        String word = parts[x];
       if(word.length() >= 5)
       {
           if(myMap.containsKey(word))
           {
              myMap.put(word, myMap.get(word) + 1);
           }
           else
           {
              myMap.put(word, new Integer(1));
           }
       }
    }

Then at the very end you can get the HashMap's internal set with myMap.entrySet(). Then interate through that set to find the first, second, third, ect most common or least common words.

Samuel Davidson
  • 783
  • 6
  • 16
  • 1
    You would also need to remove "grammar" characters such as full stops and commas which would make words bigger and also create two versions in the map. This assumes your file contains text and not just a list of words. – abcdef Mar 20 '15 at 05:15