Character Frequency

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 30
Reputation: crestaldin is an unknown quantity at this point 
Solved Threads: 0
crestaldin crestaldin is offline Offline
Light Poster

Character Frequency

 
0
  #1
Sep 3rd, 2006
I am trying to write a code to count the frequency of characters in a String;

For example if I have String str = new String("This is a test");

I want to count the number of occurrence of each character in the string.(assuming the code is case insensitive)

This is what I have at present ...

  1.  
  2. //
  3.  
  4.  
  5. public static void main(String[] args)
  6. {
  7. // TODO Auto-generated method stub
  8.  
  9. String temp = new String("This is a test message");
  10. //int[] alphabets = new int [26];
  11. int [] temp1 = new int[100];
  12. char [] s = temp.toUpperCase().toCharArray();
  13.  
  14.  
  15. for(int i=0;i<s.length;i++)
  16. {
  17. temp1[(s-'A')]++;
  18. }
  19.  
  20. for(int i =0;i<temp1.length;i++)
  21. {
  22. System.out.println(temp1);
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
  29. }


I need help on how to complete this implementation.

Thanks in advance for your help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: Character Frequency

 
0
  #2
Sep 3rd, 2006
Yeah, we're not suppose to do your homework for you bud.

Do you have to use arrays? It might be best to use a HashMap here. Using a HashMap will allow you to count only the characters that you have and nothing else. However, you will have to make a lot of objects to make it work.

You also might want to look at the String methods .toUpperCase() and .toLowerCase().
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 30
Reputation: crestaldin is an unknown quantity at this point 
Solved Threads: 0
crestaldin crestaldin is offline Offline
Light Poster

Re: Character Frequency

 
0
  #3
Sep 3rd, 2006
Originally Posted by hooknc View Post
Yeah, we're not suppose to do your homework for you bud.

Do you have to use arrays? It might be best to use a HashMap here. Using a HashMap will allow you to count only the characters that you have and nothing else. However, you will have to make a lot of objects to make it work.

You also might want to look at the String methods .toUpperCase() and .toLowerCase().
Thank you for the suggestion but ... that's actually not my homework. I just went online (http://cs.furman.edu/ccscse2002/prog...gcont_1995.pdf ) to get a website with Java problems and I'm working on them to improve on my skills.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: Character Frequency

 
0
  #4
Sep 5th, 2006
  1. HashMap freqMap = new HashMap();
  2.  
  3. //FOR loop through chars goes here
  4. if (freqMap.containsKey(character))
  5. freqMap.put(character, freqMap.get(character)+1);
  6. else
  7. freqMap.put(character, 1);

Don't expect that code to work with a copy n paste job, you'll still need to do some casting and boxing.

You can even condense that 4-line IF statement into a single line using the alternative IF structure. Once you've solved this problem successfully, I'll show you how if interested.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5279 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC