Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing the word count of every word in a file: the word count for words at the end of a line are not showing up

Goal:

Write a console program in Java that allows the user to specify a text file, opens the file, and prints a two-column table consisting of all the words in the file together with the number of times that each word appears. Words are space-delimited and case-sensitive. The table should list the words in alphabetical order.

I commented out the code that isn’t showing up in the Program Output section below. They are the last characters in their respective lines of the text file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any fixes for this?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

class TwoColTablePractice {

    public static void main(String[] args) throws IOException {

        String fileName;
        // Ask for the file name.
        System.out.println("Enter the file name:");
        // Open the file.
        Scanner scan = new Scanner(System.in);
        fileName = scan.nextLine();

        // Read the contents of the file as a stream of characters.
        FileReader fileReader = new FileReader(fileName);
        // Minimize the number of I/O operations by storing the characters in an
        // internal buffer.
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        // Create an array list of the string values.
        ArrayList<String> nameList = new ArrayList<>();

        StringBuffer sbf1 = new StringBuffer();
        int i;
        // Read the first character from the BufferedReader object.
        while ((i = bufferedReader.read()) != -1) {
            sbf1.append((char) i);
            // If the character is a space, delete it.
            if ((char) i == ' ') {
                nameList.add(sbf1.toString().replace(".", " ").replace(",", " "));
                sbf1.delete(0, sbf1.length());
            }
        }
        nameList.add(sbf1.toString().replace(".", " ").replace(",", " "));
        Map<String, Integer> counter = new HashMap<>();
        for (String string : nameList) {
            if (counter.containsKey(string)) {
                counter.put(string, counter.get(string) + 1);
            } else {
                counter.put(string, 1);
            }
        }
        Map<String, Integer> valuePairs = new TreeMap<>();
        valuePairs.putAll(counter);

        bufferedReader.close();
        fileReader.close();
        scan.close();
        // Print the two-column table consisting of all the words in the file
        // together with the number of times that each word appears.
        System.out.print("Words:");
        System.out.println(" Word Count:");
        for (String i2 : valuePairs.keySet()) {
            System.out.print(i2);
            System.out.println(valuePairs.get(i2));
        }
    }
}

Program Output:

    Enter the file name:
    
    TwoColText.txt
    
    Words: Word Count:
    
    Write 1
    
    a 3
    
    all 1
    
    allows 1
    
    alphabetical 1
    
    and 2
    
    appears   // LAST WORD ON LINE 1: WORD COUNT NOT SHOWING UP
    
    Words 1
    
    are 1
    
    case-sensitive // LAST WORD ON LINE 2: WORD COUNT NOT SHOWING UP
    
    The 1

    etc.

>Solution :

I have debugged your code. There is problem in your while loop. You are reset StringBuffer when you encounter space char, you should do same when you find '\n'. Because there is no space end of the line, it is newline.
Also, you should replace '\n' with space character.
See your fixed part of your code.

while ((i = bufferedReader.read()) != -1) {
    sbf1.append((char) i);
    // If the character is a space, delete it.
    if ((char) i == ' ' || (char) i == '\n') {
        nameList.add(sbf1.toString().replace(".", " ").replace(",", " ").replace("\n", " " ));
        sbf1.delete(0, sbf1.length());
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading