package study;
public class AboutString{
public static void main(String[] args){
String s = "AaaaABBBB/&^ccccHVGHkjhHJB16516351_JH";
int lCount = 0, uCount = 0, oCount = 0;
String s1 = "abcdefghijklmnopqrstuvwxyz";
String s2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
try{
for(int i=0; i<s.length(); i++){
if(s1.charAt(s.indexOf(i)) != -1){
lCount++;
}
else if(s2.charAt(s.indexOf(i)) != -1){
uCount++;
}
else{
oCount++;
}
}
}catch(StringIndexOutOfBoundsException e){
e.printStackTrace();
e.getMessage();
}
System.out.println("大寫:"+uCount+" "+"小寫:"+lCount+" "+"其他: "+oCount);
System.out.println();
}
}
這裡字符串怎麼會越界?
s1和s2都不如s長,所以s.indexOf(i)返回的結果可能超過s1 s2的長度,用charAt會越界