简体   繁体   English

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

[英]Java - 2d Array not filling when transferring info from file

In my program I am trying to read a file that contains Strings, booleans and doubles. 在我的程序中,我试图读取一个包含字符串,布尔值和双精度数的文件。 I'm trying to have it skip a line if it determines that it isn't a double when filling an array. 我试图让它跳过一行,如果它确定在填充数组时不是双精度型。 This is done by first checking if there is data on the line. 首先检查线路上是否有数据。 Then it determines if the first character is a number or not. 然后确定第一个字符是否为数字。 However, when I print the supposedly filled array with Arrays.deepToString it shows this: 但是,当我用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], ...

I was wondering if anyone could tell me why it isn't filling properly. 我想知道是否有人可以告诉我为什么填充不正确。 Here is the sample code: 这是示例代码:

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;
}

Here is a sample of the file (it should just fill the array with the doubles): 这是文件的一个示例(它应该只用双精度填充数组):

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

Thanks in advance. 提前致谢。

EDIT Here is the current array: 编辑这是当前数组:

[[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]]

Here is the full file: 这是完整的文件:

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

this code worked perfectly for me with your sample input 此代码对您的示例输入非常适合我

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;
}

this is how I tested it 这就是我测试的方式

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();
    }
}

and here's the output 这是输出

[[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

my function readFile implemented with a BufferedReader 我的功能readFile用BufferedReader实现

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM