The following code is attempting to Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.
The code is having this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
The main method for this code is:
public static void main(String [] args)
{
System.out.println(countX("hx1x"));
}
The actual code is:
public static int countX(String str)
{
if(str.charAt(0) != 'x')
{
if(str.indexOf('x') >= 1)
{
return countX(str.substring(1, str.length()));
}
else
{
return 0;
}
}
else
{
return 1 + countX(str.substring(1, str.length()));
}
}