I am trying to accept an input and then turn that input into Camel Case in a method using recursion, but for some reason, it keeps saying that line starting with "String a" has an error and can't output the data. Can you tell me what I did wrong?
public static String toCamelCase(String toCamel) {
String[] arr = toCamel.toLowerCase().trim().split(" ");
int i=arr.length-1;
if(i==0) {
return arr[i];
}else if(i>0) {
String a = arr[i].toUpperCase().substring(0, 1);
arr[i]= a + arr[i].substring(1);
return toCamelCase(arr[i-1])+arr[i];
}
return Arrays.toString(arr);
And this is the error message/output.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException