# Array out of bound- Recursive Method

• 11-12-2010, 08:48 PM
hpayandah
Array out of bound- Recursive Method
Code:

```public class rot13a {   public static char[] cipher (char x[], int n)   {     if (n==0){       if (x[n] >= 'a' && x[n] <= 'm') x[n] += 13;       else if (x[n] >= 'n' && x[n] <= 'z') x[n] -= 13;       else if (x[n] >= 'A' && x[n] <= 'M') x[n] += 13;       else if (x[n] >= 'N' && x[n] <= 'Z') x[n] -= 13;       return x;     }         else{       if (x[n] >= 'a' && x[n] <= 'm') x[n] += 13;       else if (x[n] >= 'n' && x[n] <= 'z') x[n] -= 13;       else if (x[n] >= 'A' && x[n] <= 'M') x[n] += 13;       else if (x[n] >= 'N' && x[n] <= 'Z') x[n] -= 13;       return cipher(x,n-1);     }   }     public static void main (String args[])   {     char str[]=ITI1120.readCharLine();     int n=str.length;     char a[];     a = cipher(str, n);     System.out.println(a[0]);   } }```
Can't seem to get where I'm going wrong in this code. Help urgently needed, thank you.
• 11-12-2010, 08:54 PM
Fubarable
Your array x has length n which means that array x holds 0 to n-1 items, and then in your code you're trying to get item x[n] which should understandably cause an array out of bounds error.
• 11-12-2010, 09:02 PM
Fubarable
One solution is to call your method with n-1:
Code:

`      a = cipher(str, n-1);`
Myself, I like to to rot13 by shifting letters over by 'a' or 'A', adding 13, getting the result modulo 26, and adding 'a' or 'A' back.