Java - 从文件传输信息时未填充 2d 数组



在我的程序中,我正在尝试读取一个包含字符串、布尔值和双精度的文件。如果它确定它在填充数组时不是双精度,我试图让它跳过一行。这是通过首先检查线路上是否有数据来完成的。然后它确定第一个字符是否是数字。但是,当我用Arrays.deepToString打印所谓的填充数组时,它显示以下内容:

[[2.33, 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], ...

我想知道是否有人可以告诉我为什么它没有正确填充。下面是示例代码:

public static double[][] readFile(String fileName) throws IOException
{
    double data[][] = new double[30][6];
    int indexRow = 0, indexCol = 0;
    File file = new File(fileName);
    Scanner input = new Scanner(file);
    while(input.hasNext())
    {
        String temp = input.nextLine();
        if(temp.trim().length() > 0)
        {
            if(Character.isDigit(temp.charAt(0)))
            {
                data[indexRow][indexCol] = input.nextDouble();
                if(indexCol < 6) indexCol++;
                if(indexRow < 30 && indexCol % 6 == 0)
                {
                    indexRow++;
                    indexCol = 0;
                }
            }
            else{}
        }   
        else
            break;
    }
    System.out.println(Arrays.deepToString(data));
    return data;
}

下面是该文件的示例(它应该只用双精度填充数组(:

Washington Capitals
3.02
2.33
85.2
30.6
28.4
True
Dallas Stars
3.23
2.78
82.3
32.0
28.9
True
St. Louis Blues
2.67
2.40
85.1
30.2
29.7
True

提前谢谢。

编辑下面是当前数组:

[[3.02, 2.33, 85.2, 30.6, 28.4], [3.23, 2.78, 82.3, 32.0, 28.9], [2.67, 2.4, 85.1, 30.2, 29.7], [2.94, 2.43, 84.4, 33.2, 29.7], [2.83, 2.44, 79.5, 28.8, 29.5], [2.85, 2.52, 80.3, 30.5, 30.8], [2.62, 2.29, 87.2, 30.3, 27.5], [2.72, 2.34, 81.4, 32.0, 27.5], [2.84, 2.62, 78.2, 28.5, 30.4], [2.77, 2.57, 84.5, 29.4, 30.4], [2.89, 2.52, 80.5, 30.4, 27.4], [2.73, 2.41, 84.0, 29.3, 28.9], [2.73, 2.6, 81.2, 30.6, 27.3], [2.57, 2.56, 80.5, 31.0, 30.7], [2.88, 2.78, 82.2, 32.0, 30.4], [2.55, 2.67, 81.5, 29.7, 29.7], [2.6, 2.49, 77.9, 29.0, 28.6], [2.39, 2.7, 84.3, 29.9, 27.6], [2.8, 2.94, 75.8, 28.4, 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.0, 0.0, 0.0, 0.0]]

这是完整文件:

Carolina Hurricanes
2.39
2.70
84.3
29.9
27.6
False
Ottawa Senators
2.80
2.94
75.8
28.4
32.8
False
New Jersey Devils
2.22
2.46
83.0
24.4
28.6
False
Colorado Avalanche
2.59
2.93
80.2
28.6
32.3
False
Montréal Canadiens
2.63
2.84
81.9
30.5
29.4
False
Buffalo Sabres
2.43
2.62
82.6
29.5
30.6
False
Winnipeg Jets
2.59
2.88
78.4
29.6
29.5
False
Arizona Coyotes
2.54
2.98
77.3
27.6
31.0
False
Calgary Flames
2.79
3.13
75.5
29.2
29.0
False
Columbus Blue Jackets
2.60
3.02
81.0
29.1
31.1
False
Vancouver Canucks
2.27
2.91
81.1
28.2
32.5
False
Edmonton Oilers
2.43
2.95
81.1
29.1
31.1
False
Toronto Maple Leafs
2.34
2.93
81.6
30.7
30.5
False

这段代码非常适合我与您的示例输入一起使用

public static double[][] readFile(String fileName) throws IOException {
    double data[][] = new double[30][6];
    int indexRow = 0, indexCol = 0;
    File file = new File(fileName);
    try (Scanner input = new Scanner(file)) {
        while (input.hasNext()) {
            String temp = input.nextLine();
            if (temp.trim().length() == 0) {
                continue;
            } else {
                if (Character.isDigit(temp.charAt(0))) {
                    data[indexRow][indexCol] = Double.parseDouble(temp);
                    if (indexCol < 6)
                        indexCol++;
                    if (indexRow < 30 && indexCol % 6 == 0) {
                        indexRow++;
                        indexCol = 0;
                    }
                } else {
                    //do something
                }
            }
        }
    }
    System.out.println(Arrays.deepToString(data));
    return data;
}

这就是我测试它的方式

public static void main(String[] args) throws IOException {
    double[][] result = readFile("array.txt");
    for (int i = 0; i < result.length; i++) {
        boolean isFirst = true;
        for (int k = 0; k < result[i].length; k++) {
            if (isFirst)
                isFirst = false;
            else
                System.out.print(", ");
            System.out.printf("%5s", result[i][k]);
        }
        System.out.println();
    }
}

这是输出

[[3.02, 2.33, 85.2, 30.6, 28.4, 3.23], [2.78, 82.3, 32.0, 28.9, 2.67, 2.4], [85.1, 30.2, 29.7, 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.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], [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.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
 3.02,  2.33,  85.2,  30.6,  28.4,  3.23
 2.78,  82.3,  32.0,  28.9,  2.67,   2.4
 85.1,  30.2,  29.7,   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.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
  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.0,   0.0
  0.0,   0.0,   0.0,   0.0,   0.0,   0.0

我的函数 readFile 使用缓冲读取器实现

public static double[][] readFile(String fileName) throws IOException {
    double data[][] = new double[30][6];
    int indexRow = 0, indexCol = 0;
    File file = new File(fileName);
    try (BufferedReader input = new BufferedReader(new FileReader(file))) {
        String temp = input.readLine();
        while (temp != null) {
            if (temp.trim().length() > 0) {
                if (Character.isDigit(temp.charAt(0))) {
                    data[indexRow][indexCol] = Double.parseDouble(temp);
                    if (indexCol < 6)
                        indexCol++;
                    if (indexRow < 30 && indexCol % 6 == 0) {
                        indexRow++;
                        indexCol = 0;
                    }
                } else {
                    //do something
                }
            }
            temp = input.readLine();
        }
    }
    System.out.println(Arrays.deepToString(data));
    return data;
}

最新更新