# Java - Counting Number of Characters Matching Each Alphabetical Letter via an Array

1. Jan 31, 2012

### prosteve037

1. The problem statement, all variables and given/known data
Here's what I have to do in a nutshell:

The package that the assignment comes in also provides tests to show if/where our code fails to produce the right responses, based on the input.

2. Relevant equations

-------------------------

3. The attempt at a solution

Here's the code I've written so far to try and solve this problem:

Code (Text):

public class Homework2Class {

private int _a;

public Homework2Class() {}

public int[] characterCounter(String s) {

int[] characterSetArray = new int[27];

_a = (int)'a';

for (int x = 0; x < characterSetArray.length; x++) {
characterSetArray[x] = _a;
_a = _a + 1;
}

for (int n = 0; n < s.length(); n++) {
char c = s.charAt(n);
Character.toLowerCase(c);

int i = (int)c;

int letterIndex = i - characterSetArray[0];

if (letterIndex >= 0 && letterIndex <= 25) {
characterSetArray[letterIndex]++;
}

else if (letterIndex > 25) {
characterSetArray[26]++;
}
}

return characterSetArray;
}

}

I ran the JUnit Test for it and all of the tests failed :P

However I did notice that the test results showed that my code returned array entries for each letter of the characterSetArray with the corresponding ASCII value for that letter. Here are the results of one of the tests:

What does this mean?

2. Feb 1, 2012

### CompuChip

Re: Java - Counting Number of Characters Matching Each Alphabetical Letter via an Arr

Well, you have the line
Code (Text):
characterSetArray[x] = _a;
which assigns the character code of "a" (which happens to be 97) to the first element and then increases it by one each time.

Apparantly your second for loop then does nothing, so these values remain.

3. Feb 2, 2012

### prosteve037

Re: Java - Counting Number of Characters Matching Each Alphabetical Letter via an Arr

Ahh, thank you. I didn't put in the other array as I had intended :P

Now revised:

Code (Text):

package hw2;

public class Homework2Class {

private int _a;

public Homework2Class() {}

public int[] characterCounter(String s) {

int[] returningArray = new int[27];

int[] characterSetArray = new int[27];

_a = (int)'a';

for (int x = 0; x < characterSetArray.length; x++) {
characterSetArray[x] = _a;
_a = _a + 1;
}

for (int n = 0; n < s.length(); n++) {
char c = s.charAt(n);
Character.toLowerCase(c);

int i = (int)c;

int letterIndex = i - characterSetArray[0];

if (letterIndex >= 0 && letterIndex <= 25) {
returningArray[letterIndex]++;
}

else if (letterIndex < 0 || letterIndex > 25) {
returningArray[26]++;
}
}

return returningArray;
}

}

I'm still getting 4 out of 7 failures though :/

Here are screens of the tests:

Test 2

Test 3

Test 5

Test 7

I think I understand what's happening in these Test Cases, though I don't understand why; I think the code isn't converting the upper-cased letters to lower-case letters for some reason.

But with the static method Characters.toLowerCase(...); shouldn't this take care of that? What's happening? What am I missing?

4. Feb 2, 2012

### haichau6990

Re: Java - Counting Number of Characters Matching Each Alphabetical Letter via an Arr

//char c = s.charAt(n);
// Character.toLowerCase(c);
These one doesnt work. Try to change them all to:
s = s.toLowerCase();
Im pretty sure that this one will work

5. Feb 4, 2012

### prosteve037

Re: Java - Counting Number of Characters Matching Each Alphabetical Letter via an Arr

Thanks so much!!! The tests all green-barred! :]