“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:4位于bt.ddd.main(ddd.java:13)
因为我最近学习了java,所以我需要一种简单的方法来解决这个问题“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:4位于bt.ddd.main(ddd.java:13),java,Java,因为我最近学习了java,所以我需要一种简单的方法来解决这个问题 public static void main(String[] args) { String test = "F: M1,“Khan lanh”,1; M2,”Trai cay dia”,3; M3,”Chuoi luoc”,1; M4,”Canh chua”,3"; String [] result= test.split("F:"); for (int i=1;i<result.
public static void main(String[] args) {
String test = "F: M1,“Khan lanh”,1; M2,”Trai cay dia”,3; M3,”Chuoi luoc”,1; M4,”Canh chua”,3";
String [] result= test.split("F:");
for (int i=1;i<result.length;i++){
String [] result1=result[i].split(";");
for (int j=0;j<result1[i].length();j++){
String [] result2= result1[j].split(",");
for(String s: result2){
System.out.println(s);
}
}
}
}
您需要将第二个
for
循环中的条件更改为如下内容:-
for (int j = 0; j < result1.length; j++) { // traverse over all elements of result1 and thus use its length
for(int j=0;j
您所做的是
另外,顺便说一句,你的第一个
for
不应该从0
开始,而应该从1
开始(刚才看到了你的字符串。1
在你的情况下应该可以。)result1[i]。length()
看起来可疑使用dubugger查看数组中有多少项。
for (int j = 0; j < result1.length; j++) { // traverse over all elements of result1 and thus use its length