Results 1 to 6 of 6
- 12-17-2011, 02:13 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Top ten most frequently occurring characters program
Hi guys.
I'm fairly new to java but I'm trying to make a simple program that reads a txt file and displays the top ten most frequently occuring characters along with the number of occurrences each.
So far I've managed to get a list of all the characters within the txt file along with the number of occurrences for each character.
What I need help on is displaying the the top ten most frequently occuring characters. I've tried sorting but I can't seem to get my head around it.
Can anyone tell me how to go about doing this?
I've posted my code below. Hope it helps!
Please remember, I'm newish to Java.
Mike
Java 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); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); String concatString=""; dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. concatString = concatString+dis.readLine(); } long numChar = file.length(); int countChar =0; int arrayChars[]=new int[(int)numChar]; for(char ch : concatString.toCharArray()) { if(ch != ' ') { if (ch <= MAX_CHAR) { counts[ch] += 1; } countChar++; } } System.out.println("Total characters: "+countChar); // print the counts for all letters & digits for(c = 0; c <= MAX_CHAR; c++) { if (counts[c] > 0 && Character.isLetterOrDigit((char)c)) { System.out.println(" " + (char)c + " " + counts[c]); } } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
- 12-17-2011, 03:07 AM #2
Re: Top ten most frequently occurring characters program
Can you explain what your thoughts are?
What does your current code do that is useful in finding the 10 most frequently occurring characters?
You have: the number of occurrences for each character.
You could go through that list of numbers and find the biggest one, and then the next biggest one etc until you have done it for 10 characters.
There are several ways to do this. Some of them depend on what you know about making classes and using methods in other classes.
The simplest way without using and of this new stuff would be to use the counts as you have them.
- 12-17-2011, 01:41 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Re: Top ten most frequently occurring characters program
Hey, thanks for the help.
I've decided to use a hash map to store my information because it seemed like an easy alternative.
So far I've got the top ten most occurring characters in the sample file. It works, I'm just wondering if there's a better way to do it. Does the code look inefficient or unreadable in any way? I'm trying to get my head around Java so any advice is welcome.
Thanks
Mike
Here's the code:
Java 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(); } } }Last edited by KardKaper; 12-17-2011 at 05:01 PM.
- 12-17-2011, 04:40 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Re: Top ten most frequently occurring characters program
Bumpsies
- 12-17-2011, 05:13 PM #5
Re: Top ten most frequently occurring characters program
Looks good to me.
- 12-17-2011, 06:33 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: Top ten most frequently occurring characters program
Is this the same as this?
Top ten most frequently occurring characters program
If yes, you really should let folks know of cross-posting, lest you wish to waste the time of those trying to help you
Similar Threads
-
Strange problem occurring with my code
By Tech2011 in forum EclipseReplies: 5Last Post: 10-13-2011, 10:30 AM -
What's wrong with this program using escape characters?
By dunboody in forum New To JavaReplies: 5Last Post: 10-09-2011, 08:59 PM -
How to make my program NOT dependent on spaces between characters?
By dragstang86 in forum New To JavaReplies: 4Last Post: 06-30-2011, 02:18 AM -
MySQL connection via Hibernate is timed out frequently
By gane in forum JDBCReplies: 2Last Post: 12-15-2010, 06:09 AM -
View the most occurring value from an arraylist
By frozensun in forum New To JavaReplies: 12Last Post: 11-01-2009, 06:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks