0

For a class assignment I have to write a method that called encodeChars that takes an array of characters and shifts each lowercase letter in the array up by one. The method is supposed to modify the array passed in and has no return value. My existing code is as follows:

 public void encodeChars(char[] change){
    for (int x = 0; 0 < change.length; x++){
        switch (change[x]){
        case('a'):
            change[x] = 'b';
            break;
        case('b'):
            change[x] = 'c';
            break;
        case('c'):
            change[x] = 'd';
            break;
        case('d'):
            change[x] = 'e';
            break;
        case('e'):
            change[x] = 'f';
            break;
        case('f'):
            change[x] = 'g';
            break;
        case('g'):
            change[x] = 'h';
            break;
        case('h'):
            change[x] = 'i';
            break;
        case('i'):
            change[x] = 'j';
            break;
        case('j'):
            change[x] = 'k';
            break;
        case('k'):
            change[x] = 'l';
            break;
        case('l'):
            change[x] = 'm';
            break;
        case('m'):
            change[x] = 'n';
            break;
        case('n'):
            change[x] = 'o';
            break;
        case('o'):
            change[x] = 'p';
            break;
        case('p'):
            change[x] = 'q';
            break;
        case('q'):
            change[x] = 'r';
            break;
        case('r'):
            change[x] = 's';
            break;
        case('s'):
            change[x] = 't';
            break;
        case('t'):
            change[x] = 'u';
            break;
        case('u'):
            change[x] = 'v';
            break;
        case('v'):
            change[x] = 'w';
            break;
        case('w'):
            change[x] = 'x';
            break;
        case('x'):
            change[x] = 'y';
            break;
        case('y'):
            change[x] = 'z';
            break;
        case('z'):
            change[x] = 'a';
            break;
        default:
            change[x] = change[x];
            break;
        }
    }
}

While I know that the switch is probably overkill for changing the incoming chars and a loop would be more efficient, my issues comes in when trying to test the code in my main method.

For the life of me I can not figure out, or find anywhere in class notes/ on the internet of how to get this method to take in an array of chars and modify it.

This goes along with testing the method, to make sure I correctly wrote the code to do as I need it. Also, sorry for the any issues that may have resulted in my inexperience of posting code to stackoverflow.

  • 5
    `char[] foo = { 'f', 'o', 'o' }; encodeChars(foo); System.out.println(java.util.Arrays.toString(foo));` – Luiggi Mendoza Oct 27 '14 at 16:24
  • 7
    Hint: `'b' == 'a' + 1` – JB Nizet Oct 27 '14 at 16:25
  • 6
    Unless you like infinite loops you may want to look at this line again as well: `for (int x = 0; 0 < change.length; x++){` – sheltem Oct 27 '14 at 16:28
  • I don't understand. Doesn't change.length find the length of the char array that the method is passed? Hence wouldn't that loop only increment x up to one less than the number of characters the char array contains? – Cremedekhan Oct 27 '14 at 16:36
  • @Cremedekhan it would if the end condition was `x < change.length`. But it is `0 < change.length`. – JB Nizet Oct 27 '14 at 16:37

3 Answers3

2

To the point: how to get this method to take in an array of chars and modify it?

To simply check if it compiles and runs:

  • Create a char[] and initialize it.
  • Call your method and pass this char[] as argument.
  • Print the contents of the char[].
  • Execute the code.

Base code:

public class YourClass {
    public void encodeChars(char[] change) {
        //implementation details...
    }
    public static void main(String[] args) {
        char[] foo = { 'f', 'o', 'o' };
        YourClass yourClass = new YourClass();
        yourClass.encodeChars(foo);
        System.out.println(java.util.Arrays.toString(foo));
    }
}

To test it:

  • Create a char[] and initialize it.
  • Call your method and pass this char[] as argument.
  • Create another char[] with the expected result of the changes.
  • Compare each value of these char[]s to verify they're equal. If not, then there's something wrong in the implementation of the method.

Base code:

char[] foo = { 'f', 'o', 'o' };
encodeChars(foo);
char[] expectedFoo = { 'g', 'p', 'p' };
for (int i = 0; i < expectedFoo.length; i++) {
    if (expectedFoo[i] != foo[i]) {
        System.out.println("Expected: " + expectedFoo[i] + ". Found: " + foo[i] + ".");
        break;
    }
}

Or in JUnit test form:

public class YourClassTest {
    @Test
    public void testEncodeChars() {
        char[] foo = { 'f', 'o', 'o' };
        encodeChars(foo);
        char[] expectedFoo = { 'g', 'p', 'p' };
        Assert.assertArrayEquals(expectedFoo, foo, "encodeChars doesn't work as expected!");
    }
}

You can also create a char[] from a String by using String#toCharArray:

char[] foo = "foo".toCharArray();

There are problems in your implementation, but that's not your exact question, so fixing them is not covered here.

Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
  • Your response was most helpful in terms of giving me the understanding I needed to complete the assignment. I appreciate the time you took to respond to my posting with such a detailed answer. – Cremedekhan Oct 27 '14 at 16:55
0

From your question it looks like you are dealing with lower case characters only. The answer is simple. Replace you switch-case with the following statement:

if(change[x] > 96 && change[x] < 122)  
{
   change[x]= change[x] + 1;
}
else if(change[x] == 122)
{
   change[x]= 97;
}  

Char in Java represents the unicode value of any character. Refer to this . Hence adding 1 to 'a' will give you 'b'. But when you get to 'z' adding 1 to it will give you the next character represented by decimal value 123 which is '{' . Hence the if else structure.

Chiseled
  • 2,172
  • 4
  • 29
  • 55
  • From OP's question: *I have to write a method that called encodeChars that takes an array of characters and shifts each lowercase letter in the array up by one*. This is, only lowercase letters should be affected. Your current `if` applies for any `char` except `'z'`. – Luiggi Mendoza Oct 27 '14 at 16:49
  • Thanks for pointing that out. Have updated my answer. Cheers !! – Chiseled Oct 27 '14 at 16:54
-2

Java passes object references by value. This means that changes you make to the array in the method will be seen outside the method.

Since you are dealing with character arrays, you can construct a string directly from it using the appropriate constructor: String(char[])

Java characters can be treated as a special integer type if you want to do math on them. If you want to add one to a char array element, you can do the following:

if(change[x] == 'z') change[x] = 'a';
else change[x]++;

You have to check the loop index rather than a constant in your loop: for (int x = 0; 0 < change.length; x++) should read for (int x = 0; x < change.length; x++).

Mad Physicist
  • 76,709
  • 19
  • 122
  • 186