简体   繁体   English

Java-扫描程序无法识别文件中的双精度值

[英]Java - Scanner won't recognize double value from file

I am trying to save values from a tab-separated file into a class. 我正在尝试将制表符分隔文件中的值保存到类中。 My problem is that my scanner will not recognize the last token as a double. 我的问题是我的扫描仪无法将最后一个令牌识别为双精度。

My file: 我的档案:

    Saint Lucia 179667  0.46
    Bosnia & Herzegovina    3503554 -0.10
    Tajikistan  9107211 2.08

The code that throws the error: 引发错误的代码:

    while (fiSc.hasNextLine()) {
        temp.add(new Country(fiSc));
    }

Constructor for Country class: 国家(地区)类的构造函数:

    public Country(Scanner fiSc) {
        name = fiSc.next();
        pop = fiSc.nextInt();
        rate = fiSc.nextDouble();
    }

Temp is a LinkedList of the type Country where I am storing the file information. Temp是存储文件信息的国家(地区)类型的LinkedList。 Country holds the name (String), population (int), and growth rate (double). 国家(Country)拥有名称(String),人口(int)和增长率(double)。 I have tried constructing the Country class by first creating the object and adding the object after, however, I was able to pinpoint the problem to the last token. 我尝试通过首先创建对象并在添加对象之后构造Country类,但是,我能够将问题精确定位到最后一个标记。

When I try this code: 当我尝试此代码时:

        while (fiSc.hasNextLine()) {
            System.out.println(fiSc.next());
            System.out.println(fiSc.nextInt());
            System.out.println(fiSc.nextDouble());
        } 

I get: 我得到:

    Saint Lucia
    179667
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at MyMain.fillList(MyMain.java:61)
    at MyMain.main(MyMain.java:11)

I think the problem is not in your code but in your input file. 我认为问题不在您的代码中,而在您的输入文件中。
Your input file looks like that every new line begins with a tab character and ends with a Newline character (invisible). 您的输入文件看起来每一行都以制表符开头,以换行符结尾(不可见)。 The problem here is that the next character after your last (double) value is not a tab but a line break (Newline). 这里的问题是,上一个(双精度)值之后的下一个字符不是制表符,而是换行符(换行符)。
Simply said, the Scanner tries to extract the values between each delimiter - in your case between tabs. 简而言之, Scanner尝试提取每个定界符之间的值-在您的情况下为制表符之间。 But when the Scanner tries to read the last value in the line, the result value for the last value in the first line (eg) would be look something like this: 0.46EOL *, this is obviously not a double value and result in a InputMismatchException . 但是,当Scanner尝试读取该行中的最后一个值时,第一行中最后一个值的结果值(例如)如下所示: 0.46EOL *,这显然不是双0.46EOL值,并导致InputMismatchException

The easiest solution to fix the problem would be to remove the leading tabs of each line and append after every last value of a line a tab too. 解决该问题的最简单解决方案是删除每行的前导制表符,并在行的每个最后值之后附加一个制表符。
For example, the first line should look like this: Saint Lucia TAB 179667 TAB 0.46 TAB (replace TAB with an actual tab). 例如,第一行应如下所示: Saint Lucia TAB 179667 TAB 0.46 TAB (用实际选项卡代替TAB )。

* EOL Newline or 'end of line' character. * EOL换行符或“行尾”字符。 More Information on Wikipedia . 有关Wikipedia的更多信息。

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

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