Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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
外环不是';t工作(java)_Java_List_File_Loops_Nested - Fatal编程技术网

外环不是';t工作(java)

外环不是';t工作(java),java,list,file,loops,nested,Java,List,File,Loops,Nested,我尝试从文件中读取一组数字,然后将第一行(这让我们知道需要处理多少行,在本例中为5行)和第七行(请注意,这只是一个示例,行的范围可能会发生更改)之间的行隔离开来,以便我可以以(0198,025,…,4315)。现在我的代码成功地将(0198,025,0316,0416)添加到列表中,但没有添加到其余的。我不确定我做错了什么。任何帮助都将不胜感激 输入示例: 5 0 1 98 2 5 3 16 4 16 1 0 13 2 47 3 3 4 40 2 0 71 1 51 3 43 4 30 3 0

我尝试从文件中读取一组数字,然后将第一行(这让我们知道需要处理多少行,在本例中为5行)和第七行(请注意,这只是一个示例,行的范围可能会发生更改)之间的行隔离开来,以便我可以以(0198,025,…,4315)。现在我的代码成功地将(0198,025,0316,0416)添加到列表中,但没有添加到其余的。我不确定我做错了什么。任何帮助都将不胜感激

输入示例:

5
0 1 98 2 5 3 16 4 16
1 0 13 2 47 3 3 4 40
2 0 71 1 51 3 43 4 30
3 0 20 1 94 4 46
4 0 1 1 10 2 28 3 15
2
2 3
2
0 1

public static void t() {
    List<String> L = new ArrayList();
    try {
        BufferedReader f = new BufferedReader(new FileReader("graph.txt"));
        String s = f.readLine();
        int nodes = Integer.parseInt(s);

        int c = -1;
        int c1 = 2;
        int c2 = 3;

        for (int i = 0; i < nodes; i++) {
            s = f.readLine();
            String z [] = s.split(" ");
            String start = z[0];

            for (int j = 0; j < z.length; j++) {
                int t = c+c1;
                int t2 = c+c2;

                if (t >= z.length) {
                    break;
                }
                String des = z[t];

                if (t2 >= z.length+1) {
                    break;
                }
                String weight = z[t2];

                L.add(start + " " + des + " " + weight);

                c1+=2;
                c2+=2;
            }
        }

        for (int i = 0; i < L.size(); i++) {
            System.out.println(L.get(i));
        }           
    } catch (Exception ex) {
        System.out.println(ex);
    }
}
5
0 1 98 2 5 3 16 4 16
1 0 13 2 47 3 3 4 40
2 0 71 1 51 3 43 4 30
3 0 20 1 94 4 46
4 0 1 1 10 2 28 3 15
2.
2 3
2.
0 1
公共静态void t(){
列表L=新的ArrayList();
试一试{
BufferedReader f=新的BufferedReader(新文件读取器(“graph.txt”);
字符串s=f.readLine();
int nodes=Integer.parseInt;
int c=-1;
int c1=2;
int c2=3;
对于(int i=0;i=z.length){
打破
}
字符串des=z[t];
如果(t2>=z.length+1){
打破
}
串重=z[t2];
L.添加(开始+下降+重量);
c1+=2;
c2+=2;
}
}
对于(int i=0;i
您只需要重置外部循环中的
C
变量

    for (int i = 0; i < nodes; i++) {
        s = f.readLine();
        String z [] = s.split(" ");
        String start = z[0];

        // Declare the variables here, otherwise code is the same
        int c = -1;
        int c1 = 2;
        int c2 = 3;

        for (int j = 0; j < z.length; j++) {
            int t = c+c1;
            int t2 = c+c2;

            if (t >= z.length) {
                break;
            }
            String des = z[t];

            if (t2 >= z.length+1) {
                break;
            }
            String weight = z[t2];

            L.add(start + " " + des + " " + weight);

            c1+=2;
            c2+=2;
        }
    }
for(int i=0;i=z.length){
打破
}
字符串des=z[t];
如果(t2>=z.length+1){
打破
}
串重=z[t2];
L.添加(开始+下降+重量);
c1+=2;
c2+=2;
}
}

它可能会工作,但节点没有您认为的值。要么调试,要么添加一些打印语句来检查变量的值相信我,我已经尝试调试了一个小时,但没有找到任何修复方法。节点也肯定是正确的值。您能读取并打印所有行吗?您刚刚看到了需要重置变量。运行第一行后,
c1
c2
永远不会重置,导致
t
t2
始终为
>z.length
非常感谢,这解决了我的问题。已经运行了将近18个小时,我不敢相信我忽略了这一点。再次感谢。