简体   繁体   English

Double.valueOf()java.lang.NumberFormatException

[英]Double.valueOf() java.lang.NumberFormatException

I encountered the following issue when trying read numbers from a csv file. 尝试从csv文件中读取数字时遇到以下问题。 The numbers are well formed and decimal points are also correct (dots): 数字格式正确,小数点也正确(点):

10;111.1;0.94
9.5;111.1;0.94
9;111.4;0.94
8.5;110.7;0.94

I read the file line by line and split each of them into three tokens, free of white spaces etc. (eg "10","111.1","0.94"). 我逐行读取文件,并将每个文件分成三个标记,没有空格等(例如“ 10”,“ 111.1”,“ 0.94”)。 In spite of this I got the exception when calling a parsing function: 尽管如此,调用解析函数时还是出现了异常:

             Double pwr = Double.parseDouble(tokens[1]);
             Double control = Double.parseDouble(tokens[0]);

             Double cos = Double.parseDouble(tokens[2]);

java.lang.NumberFormatException: For input string: "10"

When I change the order of lines, eg, 1 <--> 2, the problem persists, but now I got java.lang.NumberFormatException: For input string: "9.5" What is interesting, every time I make the above calls from the debugger level, I obtain correct values with no exception. 当我更改行的顺序(例如1 <-> 2)时,问题仍然存在,但是现在我遇到了java.lang.NumberFormatException: For input string: "9.5"有趣的是,每次我从在调试器级别,我毫无例外地获得正确的值。 It looks like a problem related to the first line of file. 似乎是与文件第一行有关的问题。

Have you any idea where the problem source is? 您知道问题根源在哪里吗?

It's probably a non-printable ASCII character 这可能是不可打印的ASCII字符

To remove this, you can simple use replaceAll method like this and use the following regex to remove \\\\P{Print} 要删除它,您可以简单地使用replaceAll这样的方法,并使用以下正则表达式删除 \\\\P{Print}

BufferedReader br = new BufferedReader(new FileReader(""));
String str=br.readLine();
str=str.replaceAll("\\P{Print}", "");

After running the following RegEx you should be able to parse the value 运行以下RegEx之后,您应该能够解析该值

=========================================================================== ================================================== ========================

To see which character it is you can try this. 要查看它是哪个字符,可以尝试此操作。

1) Read the line and print it as it is like this. 1) 阅读该行并像这样打印。

public class Test {
    public static void main(String[] args) {

        try {
            BufferedReader br = new BufferedReader(new FileReader("/path/to/some.csv"));
            String str=br.readLine();
            System.out.println(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OUTPUT: 输出:

在此处输入图片说明

2) Now copy output as it is and paste it inside a "" (double quotes) 2) 现在按原样复制输出,并将其粘贴到"" (双引号)中

在此处输入图片说明

As you can see the special character is visible now 如您所见,特殊字符现在可见

[SOLVED] Presumably it was a problem of some "hidden" zero-length character at the beginning of the file (BTW, thank you for the helpful suggestions!). [已解决]大概是文件开头存在一些“隐藏的”零长度字符的问题(顺便说一句,谢谢您的有用建议!)。 I changed the file encoding to UTF-8 (Menu > File > File Encoding) and that resolved the issue. 我将文件编码更改为UTF-8(菜单>文件>文件编码),从而解决了该问题。

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

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