(adsbygoogle = window.adsbygoogle || []).push({}); 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. A description of the required method:

/**

* @param s is the original String (1st parameter)

* @return an int[] of size 27, containing the character counts as indicated above.

*

* The count of the number of 'a' or 'A' characters must be in position 0 of the array,

* the count of the number of 'b' or 'B' characters must be in position 1 of the array,

*

* the count of the number of 'z' or 'Z' characters must be in position 25 of the array, and

* the count of all other characters must be in position 26 of the array.

*

* Some examples:

*

* if s is "", then all entries in the array must be 0.

* if s is "a", then all entries in the array must be 0 except for entry 0, which must be 1.

* if s is "Baaa!", then all entries in the array must be 0 except for:

* entry 0, which must be 3,

* entry 1, which must be 1, and

* entry 26, which must be 1.

*

* The only methods you may call on the String s are charAt(int) and length().

*

* You may use the static toLowerCase method defined in the Character class,

* which maps a char to its lower-case equivalent. For example,

* Character.toLowerCase('a') returns 'a'

* Character.toLowerCase('A') returns 'A'

* Character.toLowerCase('%') returns '%'

*

* You may, if you wish, define private helper method that you call from your

* character counting method.

*

*/

2. Relevant equations

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

3. The attempt at a solution

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

I ran the JUnit Test for it and all of the tests failed :PCode (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;

}

}

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?

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

**Physics Forums | Science Articles, Homework Help, Discussion**