Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:4位于bt.ddd.main(ddd.java:13)_Java - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:4位于bt.ddd.main(ddd.java:13)

“线程中的异常”;“主要”;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.

因为我最近学习了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.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

您所做的是 Reult1[i] .LimthHe()/Case>,它将考虑在<代码> i < /Cult> Th索引中出现的拆分字符串的长度<结果> 1/代码>数组,这是错误的。


另外,顺便说一句,你的第一个
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