繁体   English   中英

如何从文本文件扫描双打到数组?

[英]How to scan doubles from a text file into an array?

我有一个看起来像这样的文本文件:

苹果

10.1 20 11 21.3 31 12.7 22 32.4 42 13

第一行是公司名称,第二行代表公司的股价。

我想将所有的双打扫描到一个数组中,以便执行计算。

这是我的代码

public static void main(String[] args) throws FileNotFoundException {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Select A File");
    String FileName = keyboard.next();

    Scanner linReader = new Scanner(new File(FileName));

    double[] values = new double[10];

    while (linReader.hasNextLine()) {
        linReader.nextLine(); // skips company name
        String line = linReader.nextLine();

        // store following doubles into array
        int nums = 0; //

        if (linReader.hasNextDouble()) {
            values[nums] = linReader.nextDouble();
            nums++;
        }

    }
    linReader.close();
    System.out.println(Arrays.toString(values)); //prints out array 'values'
}

我选择文件,然后跳过第一行(其中包含公司名称)。

然后,我初始化一个数字以跟踪数组中的索引。 如果该行还有另一个双精度数,那么它将被添加到索引为'num'的数组中,并且'num'将增加1,这应该继续直到不再有双精度数为止。

至少这就是背后的逻辑。 但是输出总是出来

[0.0、0.0、0.0、0.0、0.0、0.0、0.0、0.0、0.0、0.0]

我不知道为什么会这样或如何解决。

我想linReader.nextDouble()在先前读取的行之后正在寻找一个double。

尝试删除对nextLine()一个调用。 如果这样做没有帮助,请使用以下算法:

linReader.nextLine(); // skips company name
String line = linReader.nextLine();
String[] strings = line.split(" ");

for (int i = 0; i < strings.length; i++) {
    try {
        values[nums++] = Double.parseDouble(strings[i]);
    } catch (NumberFormatException e) {
    } catch (ArrayIndexOutOfBoundsException e) {
        break;
    }
}

说明

原因是因为您没有使用双线,所以双精度数组具有默认值0.0。 该行:

String line = linReader.nextLine();

是问题的一部分。 当您执行以下行时:

linReader.nextLine(); // skips company name
String line = linReader.nextLine();

您希望跳过公司行的第一行( lineReader.nextLine )为Apple 没关系,但是当您这样做时,

String line = linReader.nextLine();

这将读取双打行。 然后,永远不会执行if因为文件中不再有其他要读取的内容,因此double数组的值除默认值外没有分配其他值。 打印line您将看到:

10.1 20 11 21.3 31 12.7 22 32.4 42 13

然后,您可以执行以下操作:

for(int i = 0; i < line.split(" ").length; i++) {
    values[i] = Double.parseDouble(line.split(" ")[i]);
}

而不是if语句。 然后它将正确输出:

[10.1, 20.0, 11.0, 21.3, 31.0, 12.7, 22.0, 32.4, 42.0, 13.0]

上面的代码执行传统的C风格的for循环。 它遍历通过执行line.split(" ")创建的double数组,该数组将空间中的所有值拆分为一个数组。 这将创建:

["10.1", "20", "11, "21.3", "31", "12.7", "22", "32.4", "42", "13"]

接下来,您将遍历所有对象,使用当前迭代或索引访问values数组,并将其分配给上述字符串数组中当前元素的已解析double。 您可以格式化,并根据需要捕获任何异常。


TL; DR

这是while循环中的最终代码:

String companyName = linReader.nextLine(); // if you want the company name, this stores it
String line = linReader.nextLine(); // this contains all double values in a string

for(int i = 0; i < line.split(" ").length; i++) { // loops over all doubles in split string array
    values[i] = Double.parseDouble(line.split(" ")[i]); // parses them as doubles and then stores to double array
}

也许您可以像这样更改代码:

public static void main(String[] args) throws FileNotFoundException {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Select A File");
    String FileName = keyboard.next();

    Scanner linReader = new Scanner(new File(FileName));

    Vector<Double> values = new Vector<>();

    while (linReader.hasNextLine()) {
        linReader.nextLine(); // skips company name
        String line = linReader.nextLine();
        String _line[] = line.split(" ");

        for (String item : _line) {
            if (!item.isEmpty()) {
                values.add(Double.parseDouble(item));
            }
        }
    }
    linReader.close();
    System.out.println(values.toString()); //prints out array 'values'
}

暂无
暂无

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

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