Question

In: Computer Science

Java 20 most frequent words in a text file. Words are supposed to be stored in...

Java 20 most frequent words in a text file. Words are supposed to be stored in array that counts eah word.

Write a program that will read an article, parse each line into words, and keep track of how many times each word occurred. Run this program for each of the two articles and print out the 20 most frequently appearing words in each article.

You may think you need to use a StringTokenizer, but it turns out that is old programming. The better thing to use is

string.split(regex)

Example of text to use:

If the subsidies vanish, low-income Americans who obtain insurance through Obamacare 
online marketplaces where insurers can sell policies would face higher insurance premiums 
and out-of-pocket medical costs. It would particularly hurt lower-middle-class families 
whose incomes are still too high to qualify for certain government assistance.

About 10 million people are enrolled in Obamacare through its online marketplaces, and 
most receive subsidies. Trump’s action came just weeks before the period starting on 
Nov. 1 when individuals have to begin enrolling for 2018 insurance coverage through 
the law’s marketplaces.

Solutions

Expert Solution

package march11;

import java.util.*;

public class FrequentWord {
    static String artical1 = "If the subsidies vanish, low-income Americans who obtain insurance through Obamacare \n" +
            "online marketplaces where insurers can sell policies would face higher insurance premiums \n" +
            "and out-of-pocket medical costs. It would particularly hurt lower-middle-class families \n" +
            "whose incomes are still too high to qualify for certain government assistance.";

    static String artical2 = "About 10 million people are enrolled in Obamacare through its online marketplaces, and \n" +
            "most receive subsidies. Trump’s action came just weeks before the period starting on \n" +
            "Nov. 1 when individuals have to begin enrolling for 2018 insurance coverage through \n" +
            "the law’s marketplaces.";


    public static void main(String[] args) {
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();

        String[] artial1Array = artical1.split("\\s+");
        String[] artial2Array = artical2.split("\\s+");


        ////add array1
        for (int i = 0; i < artial1Array.length; i++) {
            String word = artial1Array[i];
            if (stringIntegerHashMap.containsKey(word)) {
                stringIntegerHashMap.put(word, stringIntegerHashMap.get(word) + 1);
            } else {
                stringIntegerHashMap.put(word, 1);
            }
        }


        ////add array2
        for (int i = 0; i < artial2Array.length; i++) {
            String word = artial2Array[i];
            if (stringIntegerHashMap.containsKey(word)) {
                stringIntegerHashMap.put(word, stringIntegerHashMap.get(word) + 1);
            } else {
                stringIntegerHashMap.put(word, 1);
            }
        }

        //////Print 20
        stringIntegerHashMap= getSortedHashMapByValue(stringIntegerHashMap);
        int count = 0;
        for (String word : stringIntegerHashMap.keySet()) {

            System.out.println(word + " " + stringIntegerHashMap.get(word));

            if (count == 20) {
                break;
            }
            count++;
        }


    }

    private static HashMap getSortedHashMapByValue(HashMap map) {
        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o2)).getValue())
                        .compareTo(((Map.Entry) (o1)).getValue());
            }
        });

        HashMap sortedHashMap = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            sortedHashMap.put(entry.getKey(), entry.getValue());
        }
        return sortedHashMap;
    }


}

////////output/////////


Related Solutions

Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
Write a method that returns the 200tth most frequent word and its frequency from a text...
Write a method that returns the 200tth most frequent word and its frequency from a text file. Use the best text reading method with a merge sort. and/or other techniques. The method signature: public static Integer countFAST(String fileName) throws Exception {...} The function takes the text file name.
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT