Top ten most frequently occurring characters program
Hey everyone.
I've written a program that counts how many characters are in a txt file (excluding spaces) and displays the top ten most frequently occuring characters.
Can you have a look and tell me if it could be done better? e.g. how the code could be improved.
I'm very new to Java so any advice would be very helpful.
Here's my code.
Code :
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class CodifyTest {
public static final int MAX_CHAR = 65535;
public static void main(String[] args) {
int c;
long total;
int counts[];
counts = new int[MAX_CHAR+1];
File file = new File("Sample.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try
{
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
String concatString="";
dis = new DataInputStream(bis);
while (dis.available() != 0) {
concatString = concatString+dis.readLine();
}
long numChar = file.length();
int countChar =0;
for(char ch : concatString.toCharArray())
{
if(ch != ' ')
{
countChar++;
}
}
System.out.println("Total characters: "+countChar);
Map<Character, Integer> m = new HashMap<Character, Integer>();
for(char ch : concatString.toCharArray()) {
if (!Character.isLetter(ch)) continue;
if (m.containsKey(ch)) {
m.put(ch, m.get(ch) + 1);
} else {
m.put(ch, 1);
}
}
List<Map.Entry> list = new ArrayList<Map.Entry>(m.entrySet());
Collections.sort(list, new Comparator<Map.Entry>() {
public int compare(Map.Entry e1, Map.Entry e2) {
Integer i1 = (Integer) e1.getValue();
Integer i2 = (Integer) e2.getValue();
return i2.compareTo(i1);
}
});
int count=0;
for(Map.Entry e : list) {
System.out.println(" " + e.getKey()+ " (" + e.getValue() + ")");
count++;
if(count==10)
{
break;
}
}
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Re: Top ten most frequently occurring characters program
Very small point: Close the file I/O classes as soon as you don't need them.
Move most of the code out of the try{}catch block
Re: Top ten most frequently occurring characters program