Results 1 to 6 of 6
  1. #1
    KardKaper is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Lightbulb 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();
        }
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default 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.

  3. #3
    KardKaper is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default 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 06:01 PM.

  4. #4
    KardKaper is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Re: Top ten most frequently occurring characters program

    Bumpsies

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Top ten most frequently occurring characters program

    Looks good to me.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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

  1. Strange problem occurring with my code
    By Tech2011 in forum Eclipse
    Replies: 5
    Last Post: 10-13-2011, 11:30 AM
  2. Replies: 5
    Last Post: 10-09-2011, 09:59 PM
  3. Replies: 4
    Last Post: 06-30-2011, 03:18 AM
  4. Replies: 2
    Last Post: 12-15-2010, 07:09 AM
  5. View the most occurring value from an arraylist
    By frozensun in forum New To Java
    Replies: 12
    Last Post: 11-01-2009, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •