将整数从文件传递到数组

时间:2015-01-07 14:37:08

标签: java arrays file integer

有人可以解释一下为什么这段代码不起作用? 我的阵列没有充满任何东西-.-

    Integer[] tab1 = new Integer[401];
    int[][] tab2 = new int[20][20];
    File fr;
    int i = 0, c = 0;
    fr = new File("problem11");
    Scanner sc;
    try {
        sc = new Scanner(fr);
        while (sc.hasNext()) {
            // System.out.printf("%d ", sc.nextInt());
            tab1[i] = sc.nextInt();

            i++;
            System.out.print(tab1[i]);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:3)

您正在显示尚未填充的下一个数组元素

i++;
System.out.print(tab1[i]);

应该是

System.out.print(tab1[i++]);