943,957 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 10315
  • Java RSS
Sep 3rd, 2006
0

Character Frequency

Expand Post »
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 ...

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
crestaldin is offline Offline
30 posts
since Mar 2005
Sep 3rd, 2006
0

Re: Character Frequency

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().
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 3rd, 2006
0

Re: Character Frequency

Click to Expand / Collapse  Quote originally posted by hooknc ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
crestaldin is offline Offline
30 posts
since Mar 2005
Sep 5th, 2006
0

Re: Character Frequency

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jun 2nd, 2010
0
Re: Character Frequency
Try this..

public void getFrequencyInString(){
String str = "apple";
HashMap hm = new HashMap();
for(int i = 0; i<str.length(); i++){
char c = str.charAt(i);
if(hm.get(c)!= null){
hm.put(c, (Integer)(hm.get(c))+1);
}else{
hm.put(c, 1);
}
}
System.out.println(hm.entrySet());
}
Output: [e=1, p=2, a=1, l=1]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vijay1214 is offline Offline
1 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Parsing Error?
Next Thread in Java Forum Timeline: HANOI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC