1. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7

## help with arrays

i am learning about arrays, and have this assignment:

Counts the frequencies of letters A-Za-z in a string
@param str a string
@return an array of 26 counts. The i-th count is the number of occurrences
of 'A' + i or 'a' + i.

and here is my coding so far:
Java Code:
```	int[]count = new int[26];
int counter = 1;
int number = 0;
char[]alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

for (int i = 0; i < str.length(); i++)
{

if (Character.isLetter(str.charAt(i)))
{
counter++;

while (alphabet[i] == str.charAt(i))
{
counter++;
}

}

count[i] = counter;
}

return count;```
i am thinking that i need to update my "i" for both my alphabet array, and the string, charAt.

2. What errors are you getting?(if compiler errors copy and paste the exact message, if logical show output and expected output)

Also, be sure to ask specific questions.

3. You are making this harder than it needs to be. A little hint:
Java Code:
```char c = 'a';
String s = "abz";
for(int index = 0; index < s.length(); index++) {
System.out.println((int) s.charAt(index) - c);
}```

4. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
ok, so i put in a string, and will get consecutive numbers, such as 2, 3, 5, 6, 6, 7.

and thanks for the quick response!

what is the "- c"? i am confused, because i would think that that would be a number.
i get 0, 1, 25?

5. Originally Posted by droidus
what is the "- c"? i am confused, because i would think that that would be a number.
i get 0, 1, 25?
Exactly. For 'a' the result is 0, for 'b' it is 1, for 'c' it is 2, ..... for 'z' it is 25. Do those numbers ring any bells? Array indicies perhaps.

6. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
oh, ok... those are just the indexs then. i'll see what i can do with this...

7. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7

Java Code:
```Where
index number (found from code above) = number of index in array, update counter (+1) for that array slot```

8. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
got it! here is what i have:

Java Code:
```		char c = 'a';
int test = 0;
int[]letters = new int[26];
int counter = 0;

for(int index = 0; index < str.length(); index++) {
test = ((int) str.charAt(index) - c);
letters[test] = letters[test]+1;
}

for (int index=0; index < 26; index++) {
System.out.print(letters[index] + ", ");
}```
output (for hello):
Java Code:
`0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,`
how do i remove the last comma?
Last edited by droidus; 04-17-2011 at 08:07 PM.

9. Your current algorithm is to print a number followed by a comma, except the last one. The problem is how do you know when you get to the last one.

A better and easier algorithm is to print a comma before the number except the first one.

10. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
with

Java Code:
```for (int index=0; index < 26; index++) {
counter++;
if (counter == 1) {
System.out.print(letters[index] + ", ");
}
else {
System.out.print(", " + letters[index]);
}
}```
i get:

Java Code:
`0, , 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0`

11. You need to refine the algorithm. You always want to print the number so remove that from the if statement.
Java Code:
```loop {
if not first number {
print comma
}
print number```
By the way you can use the loop control variable for this and get rid of the counter.

12. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
when i run this, and return letters, i get some weird string. here is my code, and my output:

Java Code:
```char c = 'a';
int test = 0;
int[]letters = new int[26];
int counter = 0;

for(int index = 0; index < str.length(); index++) {
test = ((int) str.charAt(index) - c); // Gets the string letter position in the alphabet
letters[test] = letters[test]+1; // Gets the position in the array, and adds one to that index value
}```
Java Code:
`[I@3e25a5`
what is going on here??

13. Member
Join Date
Apr 2011
Posts
12
Rep Power
0
I believe that's outputting a memory address (usually because you're not indexing the array correctly)

I'm at work at the moment (so can't look too much in to it), if nobody has responded with a fix before I get home (a couple of hrs) then I'll have a look at it then

14. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
it sounds like it's the uppercase "H" for hello. i get an java.lang.ArrayIndexOutOfBoundsException error.

15. Member
Join Date
Apr 2011
Posts
12
Rep Power
0
Originally Posted by droidus
it sounds like it's the uppercase "H" for hello. i get an java.lang.ArrayIndexOutOfBoundsException error.
Java Code:
`for(int index = 0; index < str.length(); index++)`
To do with that perhaps?

.length results the total number of indexes in the array. Although your last index would be .length-1 (remember - because an array starts from position 0)

16. Originally Posted by ivlatt
Java Code:
`for(int index = 0; index < str.length(); index++)`
To do with that perhaps?

.length results the total number of indexes in the array. Although your last index would be .length-1 (remember - because an array starts from position 0)
That has nothing to do with it; that simple calculation assumes that the characters are in the range 'a' ... 'z'. If you subtract an 'a' from the character the index value is in the range 0 ... 25. An uppcase character is out of that range so the result is a negative index value which results in an AIOOBE.

kind regards,

Jos

17. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
ok. so how would i get around this? would i just have to use a switch statement?

18. Originally Posted by droidus
ok. so how would i get around this? would i just have to use a switch statement?
Nope, a switch statement is so old fashioned; think about it: do you just want statistics (frequencies) of lower case letters? If so, you have to exclude any other character from your input. If not, you either have to use a much larger array (where every possible character can be a index) or abandon the array approach entirely. The choice is up to you.

kind regards,

Jos

19. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
i have to return an array, and it can only have 26 values (for each letter).

20. Convert your String to lowercase.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•