1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
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

  1. Jan 31, 2012 #1
    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:

    Tests.jpg

    What does this mean?
     
  2. jcsd
  3. Feb 1, 2012 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Feb 2, 2012 #3
    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
    Test2.jpg

    Test 3
    Test3.jpg

    Test 5
    Test5.jpg

    Test 7
    Tests-1.jpg

    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?
     
  5. Feb 2, 2012 #4
    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
     
  6. Feb 4, 2012 #5
    Re: Java - Counting Number of Characters Matching Each Alphabetical Letter via an Arr

    Thanks so much!!! The tests all green-barred! :]
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java - Counting Number of Characters Matching Each Alphabetical Letter via an Array
  1. Arrays in Java (Replies: 3)

  2. Java class Arrays (Replies: 1)

Loading...