-1

I want to list every unique word in a text file and how many times every word is found in it. I tried using an if cycle but I'm not sure how to eliminate the already listed words after they are being counted.

for (int i = 0; i < words.size(); i++) {
    count = 1;
    //Count each word in the file and store it in variable count 
    for (int j = i + 1; j < words.size(); j++) {
        if (words.get(i).equals(words.get(j))) {
            count++;
        }

    }
    System.out.println("The word " + words.get(i) + " can be 
    found " + count + " times in the file.");
}

The contents of the text file is "Hello world. Hello world.", and the program will print the following: The word Hello can be found 2 times in the file. The word world can be found 2 times in the file. The word Hello can be found 1 times in the file. The word world can be found 1 times in the file.

Anil
  • 655
  • 1
  • 11
  • 25

3 Answers3

1

I would suggest to leverage a HashMap to solve this problem. simply put, HashMap is a key value pair that hashes the keys and has a search complexity of O(1).

Iterate the list of words only once and keep on storing the encountered word in a HashMap. when you encounter a word, check if it already exists in the HashMap. If it does not exist, add it to the map with key as the word itself and value as 1. if The word alrady exists, Increase the value by 1.

After completing the iteration, the HashMap would contain key value pairs of unique words vs their count !!

just in case if you are not aware of maps in java - https://www.javatpoint.com/java-hashmap

nandan aggarwal
  • 136
  • 1
  • 5
0

You could do this :

public void printWordOccurence(String filePath) throws FileNotFoundException {
        if (filePath.isEmpty())
            return;

        File file = new File(filePath);
        Scanner input = new Scanner(file);
        HashMap<String, Integer> wordOccurence = new HashMap<>();


        while (input.hasNext()) {
            wordOccurence.merge(input.next(), 1, Integer::sum);
        }

        for (String word : wordOccurence.keySet()) {
            System.out.println(word + " appears " + wordOccurence.get(word) + " times");
        }
    }
Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
0

You need to use an ArrayList to store the already found words, and after that, you need to check every word in the file, whether it is present within the ArrayList or not. If the word is present inside the ArrayList, you need to ignore that word. Otherwise, add that word to the ArrayList. A sample code for you:

ArrayList<String> found_words=new ArrayList<String>();
public static void main(String arguments[])
{
    String data=""; //data from your file
    String[] words=data.split("\\s"); //split the string into individual words
    for(int i=0;i<words.length;i++)
    {
        String current_word=words[i];
        if(!is_present(current_word))
        {
            found_words.add(current_word);
            int count=1;
            for(int j=i+1;j<words.length;j++)
            {
                if(words[j].equals(words[i]))
                    ++count;
            }
            System.out.println("The word "+current_word+" can be found "+count+" times in the file.");
        }
    }
}
static boolean is_present(String word)
{
    for(int i=0;i<found_words.size();i++)
    {
        if(found_words.get(i).equals(word))
            return true;
    }
    return false;
}
Puspam
  • 2,137
  • 2
  • 12
  • 35