Got this assignment due a few weeks later and since I am done with the up coming assignment, decided to try out the next one early rather than rush later. First part of it requires me to do a character count on a text document which i will have to use huffman coding to encode.
Below is my code that i created to count and display the characters and its frequency in the document.
The code has no errors and from the first run, it looks like everything went ok with it displaying the characters and the count. But just to make sure I got it right, I used the unix command "wc" on the document and it seems I have missing characters or something.
Unix gives:
$ wc DecOfInd.txt
29 1369 8458 DecOfInd.txt
My program gives:
Total Number of Characters: 8429
Any kind soul would like to help me find out whats wrong with my code as I seem to have 29 missing characters.
Many thanks in advance.
Kenneth :)
Below is my code that i created to count and display the characters and its frequency in the document.
Code:
import java.io.*;
import java.util.*;
public class SourceModel
{
public static void main(String [] Args)
{
int j;
String str;
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
try
{
BufferedReader in = new BufferedReader(new FileReader("DecOfInd.txt"));
while ((str = in.readLine()) != null)
{
for(int i = 0; i < str.length(); i++)
{
if(!m.containsKey((int)str.charAt(i)))
{
m.put((int)str.charAt(i),1);
}
else
{
j = m.get((int)str.charAt(i));
j++;
m.put((int)str.charAt(i),j);
}
}
}
}
catch (IOException e)
{
}
System.out.println();
System.out.println(m.size() + " distinct letters:");
System.out.println(m);
int count = 0;
for(int i :m.keySet())
{
System.out.println(i + " = " + m.get(i));
count += m.get(i);
}
System.out.println("Total Number of Characters: "+count);
}
}
Unix gives:
$ wc DecOfInd.txt
29 1369 8458 DecOfInd.txt
My program gives:
Total Number of Characters: 8429
Any kind soul would like to help me find out whats wrong with my code as I seem to have 29 missing characters.
Many thanks in advance.
Kenneth :)
Comment