Thursday, 12 September 2013

Count the occurrence of alphabetical letters + numbers in a string and print this graphically

Count the occurrence of alphabetical letters + numbers in a string and
print this graphically

DISCLAIMER; THIS IS HOMEWORK :)
I have a string, I want to count the occurrence of all letters and numbers
and want to create a graph so I can see the occurrence graphically.
So for example:
String sentence = "ABC ABC ABC 123"
A (3) * * *
B (3) * * *
C (2) * *
D
..
..
My way of thinking:
count all numbers and letters in the string
print all asterisks times this number (unfortunately I can't multiple a
string with an integer in JAVA :( )
I think there are to ways of counting the chars, I can either use the
charAt() method or toCharArry() and loop thought the string or array and
count the letters.
For example:
aCounter = 0;
bCounter = 0;
char ch = sentence.charAt(i);
for (i = 0; i < sentence.length(); ++i) {
if (ch == 'a') {
aCounter++;
} if (ch == 'b') {
bCounter++;
}
However, I have multiple problems with this approach:
I would have a to make a lot of integer aCounter till zCounter plus
0counter - 9 counter
I would have to make another for loop to print the asterisks!
I'm not asking for an set answer here, I'm just looking for some good
directions, because I'm stuck.
Thanks :)

No comments:

Post a Comment