Java 2D阵列打印问题

时间:2016-03-18 16:53:06

标签: java arrays multidimensional-array

所以我已经看了一段时间,似乎无法解决这个问题......

基本上,用户输入了一个txt文件(每行最多50行和20个整数),我将其转换为2D int[][]

所以,如果输入是:

1 2 3 4 5 6
54 67 66
45
34 54 2

2D数组应如下所示:

1  2  3  4  5  6
54 67 66 0  0  0
45 0  0  0  0  0
34 54 2  0  0  0

我目前正在获得以下输出:

[1, 2, 3, 4, 5, 6, 54, 67, 66, 45, 2, 3, 34, 54, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

除了数字全部进入第一行之外,这没什么问题 - 他们不应该这样做。

这是我的代码(Functions类):

package com.company;

import java.io.File;
import java.util.Scanner;

public class Functions {
    public int[][] readFile(File file) {

        try {

            Scanner scn = new Scanner(file);
            Scanner scn2 = new Scanner(file);

            //set initial count (of rows) to zero
            int maxrows = 0;

            //sets columns to 20 (every row has 20 integers - filled w zeros if not 20 inputted)
            int maxcolumns = 20;

            // goes through file and counts number of rows to set array parameters
            while (scn.hasNextLine()) {
                maxrows++;
                scn.nextLine();
            }

                // create array of counted size
                int[][] array = new int[maxrows][maxcolumns];

                //new scanner to reset
                Scanner scan1 = new Scanner(file);

                //places integer one by one into array
                for (int row = 0; row < maxrows; row++)
                    for (int column = 0; column < maxcolumns; column++) {
                        if (scan1.hasNext())
                            array[row][column] = scan1.nextInt();
                        else {
                            break;
                        }
                    }
                return array;
        }
        // general exception
        catch(Exception e){
            System.out.println("PROBLEM");
            e.printStackTrace();
            //returns null array
            return null;

        }

    }
}

主类:

package com.company;

import java.io.*;
import java.io.IOException;
import java.util.Arrays;
import java.io.File;



public class Main {
    public static void main(String[] args) throws IOException {
        //Creates new class2 object
        Functions o = new Functions();
        //Creates new file object
        File file = new File("src/com/company/UserInput");
        //Takes in file object as parameter
        int[][] array = o.readFile(file);

        //prints as an array
        for (int i=0; i < array.length; i++)
            System.out.println(Arrays.toString(array[i]));
    }
}

2 个答案:

答案 0 :(得分:3)

问题在于:

for (int row = 0 ; row < maxrows ; row++) {
    for (int column = 0 ; column < maxcolumns ; column++) {
        if (scan1.hasNext()) // ← *here*
            array[row][column] = scan.nextInt();
        else {
            break;
        }
    }
}

scan1.hasNext()不会在该行的末尾停止,它将返回true,直到它耗尽整个文件。相反,你应该做那样的事情:

for (int row = 0 ; row < maxrows ; row++) {
    Scanner lineScan = new Scanner(scan1.nextLine());

    for (int column = 0 ; lineScan.hasNextInt() ; column++) {
        array[row][column] = lineScan.nextInt();
    }
}

答案 1 :(得分:0)

当您使用“scan1.nextInt();”时,您忽略了新行,因为它全部在同一行。 你可以做的是使用:

Scanner scan1 = new Scanner(file).useDelimiter("\\||\\n");
while (scan1.hasNext()) {
                String nextValue = scan1.next();
                System.out.println("nextValue " + nextValue);
}

你有: nextValue 1 2 3 4 5 6 nextValue 54 67 66 nextValue 45 nextValue 34 54 2

您可以拆分此行并进行手动控制