Results 1 to 3 of 3
 11122010, 08:48 PM #1Member
 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,n1); } } 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]); } }

Your array x has length n which means that array x holds 0 to n1 items, and then in your code you're trying to get item x[n] which should understandably cause an array out of bounds error.

One solution is to call your method with n1:
Java Code:a = cipher(str, n1);
Similar Threads

Array index Out of bound Exception
By nitin_daviet88 in forum New To JavaReplies: 9Last Post: 07282010, 06:32 AM 
Array Index Out of bound exception
By abhijit in forum NetworkingReplies: 7Last Post: 09252009, 08:25 PM 
Recursive method using int array, help needed
By chupalo17 in forum New To JavaReplies: 4Last Post: 09082009, 12:15 AM 
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02162008, 10:10 PM 
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02162008, 09:52 PM
Bookmarks