我正在尝试创建一种递归方法来查找字符串中大写字母的出现次数。我的代码如下:
public class findUppercase {
public static int searchUppercase(String s, int high) {
if (s.length() == 1) {
if (Character.isUpperCase(s.charAt(0)))
return 1;
else
return 0;
} else if (Character.isUpperCase(s.charAt(high - 1)) )
return 1 + searchUppercase(s, s.length() - 1);
else
return searchUppercase(s, s.length() - 1);
}
public static int searchUppercase(String s) {
return searchUppercase(s, s.length());
}
public static void main(String[] args) {
String a = "ABCmdsaA";
System.out.println(searchUppercase(a));
}
}
我得到错误:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Character.getType(Character.java:6924)
at java.lang.Character.isUpperCase(Character.java:5518)
at java.lang.Character.isUpperCase(Character.java:5488)
at findUppercase.searchUppercase(findUppercase.java:9)