I am trying to use a recursive method to change strings to char arrays but i am getting an error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
I want to solve this problem only using recursive method(not loop or toChar method)
public class Recur {
public char[] stringTochar(String str)
{
if (str != null && str.length() > 0)
{
System.out.println(str.charAt(0)) ;
stringTochar(str.substring(1));
}
return stringTochar(str.substring(1)) ;
}
}
public class Tester {
public static void main(String[] args) {
Recur recur= new Recur ();
recur.stringTochar("this is a test");
}
}
String#toCharArray, and in particular, why reinvent it using recursion?char[]return type.