# Thread: Array out of bound- Recursive Method

1. Member
Join Date
Nov 2010
Posts
1
Rep Power
0

## Array out of bound- Recursive Method

Java 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[])
{
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.

2. 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.

3. One solution is to call your method with n-1:
Java 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•