0

I'm working on a Huffman project and I've got the basics sort of working. It counts the characters in a given string and displays them; however, I want it to read from a file rather than manually giving it a string to print.

My code is below, any advice on making it print the contents of a text file would be extremely useful.

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

    String message = "Hello";

    // Convert the string to char array

    char[] msgChar = message.toCharArray();
    ArrayList<Character> characters = new ArrayList<Character>();

    /*
     * Get a List of all the chars which are present in the string No
     * repeating the characters!
     */
    for (int i = 0; i < msgChar.length; i++) {
        if (!(characters.contains(msgChar[i]))) {
            characters.add(msgChar[i]);
        }
    }

    System.out.println(message);
    System.out.println("");

    /* Count the number of occurrences of Characters */
    int[] countOfChar = new int[characters.size()];

    /* Fill The Array Of Counts with one as base value */
    for (int x = 0; x < countOfChar.length; x++) {
        countOfChar[x] = 0;
    }

    /* Do Actual Counting! */
    for (int i = 0; i < characters.size(); i++) {
        char checker = characters.get(i);
        for (int x = 0; x < msgChar.length; x++) {
            if (checker == msgChar[x]) {
                countOfChar[i]++;
            }
        }
    }

    /* Sort the arrays is descending order */
    for (int i = 0; i < countOfChar.length - 1; i++) {
        for (int j = 0; j < countOfChar.length - 1; j++) {
            if (countOfChar[j] < countOfChar[j + 1]) {
                int temp = countOfChar[j];
                countOfChar[j] = countOfChar[j + 1];
                countOfChar[j + 1] = temp;

                char tempChar = characters.get(j);
                characters.set(j, characters.get(j + 1));
                characters.set(j + 1, tempChar);
            }
        }
    }

    /* Print Out The Frequencies of the Characters */
    for (int x = 0; x < countOfChar.length; x++) {
        System.out.println(characters.get(x) + " - " + countOfChar[x]);
    }

}
}
Hoxephos
  • 71
  • 1
  • 1
  • 6
  • Possible duplicate of [How do I create a Java string from the contents of a file?](http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – Vrashabh Irde Nov 22 '16 at 22:42

2 Answers2

0
String line;
try (
    InputStream fis = new FileInputStream("file_name");
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(isr);
) {
 while ((line = br.readLine()) != null) {
    // Deal with the line
}
}

A cleaner way would be
List<String> lines = Files.readAllLines(path);

seppdepp3
  • 13
  • 2
0

As I don't know which version of java you are using, this method will resolve:

private static String readFromFile(String filename) {
    try {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = reader.readLine()) != null)
            sb.append(line).append("\n");
        reader.close();
        return sb.toString();
    } catch (IOException e) {
        System.err.println("File " + filename + " cannot be read: " + e.getMessage());
        return null;
    }
}

And in your code, you can use it like this:

public static void main(String[] args) {
    String message = readString("testfile.txt");
    if(message == null)
        return; // cannot read!
Wilfredo Pomier
  • 1,091
  • 9
  • 12