| | |
Character Frequency
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 30
Reputation:
Solved Threads: 0
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 ...
I need help on how to complete this implementation.
Thanks in advance for your help.
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)
// public static void main(String[] args) { // TODO Auto-generated method stub String temp = new String("This is a test message"); //int[] alphabets = new int [26]; int [] temp1 = new int[100]; char [] s = temp.toUpperCase().toCharArray(); for(int i=0;i<s.length;i++) { temp1[(s-'A')]++; } for(int i =0;i<temp1.length;i++) { System.out.println(temp1); } } } }
I need help on how to complete this implementation.
Thanks in advance for your help.
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
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().
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().
•
•
Join Date: Mar 2005
Posts: 30
Reputation:
Solved Threads: 0
•
•
•
•
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().
•
•
Join Date: Mar 2004
Posts: 802
Reputation:
Solved Threads: 40
Java Syntax (Toggle Plain Text)
HashMap freqMap = new HashMap(); //FOR loop through chars goes here if (freqMap.containsKey(character)) freqMap.put(character, freqMap.get(character)+1); else 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.
![]() |
Similar Threads
- C++ (C++)
- Word Frequency Counter Help (Java)
- Frequency of characters entered. (C)
- character processing (C)
Other Threads in the Java Forum
- Previous Thread: String Splitter
- Next Thread: how to move an jlabel icon from one jlabel to another jlabel
Views: 5279 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation binary bluetooth chat chooser class classes client code component converter database detection digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle os pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows





