Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Top ten most frequently occurring characters program

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Top ten most frequently occurring characters program

    Is this the same as
    Top ten most frequently occurring characters program
    If yes, I highly recommend you read the following:
    http://www.javaprogrammingforums.com...s-posting.html

Similar Threads

  1. Character Count Frequently
    By ndundupan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2011, 09:07 PM
  2. Random Errors Occurring When Running Game in Eclipse
    By WhenThCome4Me in forum Java IDEs
    Replies: 22
    Last Post: September 1st, 2011, 06:29 AM
  3. [SOLVED] Strings and Characters
    By av8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 4th, 2011, 05:39 PM
  4. Java Frequently asked questions
    By lakshmiv92 in forum The Cafe
    Replies: 1
    Last Post: October 19th, 2010, 05:20 AM
  5. count how frequently each character occurs
    By sam in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:16 PM