在我尝试写一个翻转字符串中某一段的长度大于4的时候,发现出现了报错,请问一下应该如何解决呢
import java.util.Scanner;
public class Spin {
public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// String s=sc.nextLine();
System.out.println(spinString("this is a test"));
}
public static String spinString (String sentence){
String[] str = sentence.split(" ");
String s=null;
StringBuilder sb=null;
for (int i = 0; i < str.length; i++) {
if (str[i].length()>=5) {
str[i] = new StringBuffer(str[i]).reverse().toString();
}
sb=new StringBuilder(s).append(str[i]);
}
s=sb.toString();
return s;
}
}