简体   繁体   English

将字符串解析为Double时,数字格式异常

[英]Number Format Exception on a string when parsing it as a Double

I'm running into a problem of reading from a text file off a web server, and parsing that string to a Double to use for subsequent things. 我遇到了从Web服务器上读取文本文件并将该字符串解析为Double以用于后续操作的问题。 This is a sample line from the web server I'm reading from. 这是我正在读取的Web服务器中的示例行。

Sample line: 样品线:

"00501","+40.922326","-072.637078","HOLTSVILLE","NY","SUFFOLK","UNIQUE" “ 00501”,“ + 40.922326”,“-072.637078”,“ HOLTSVILLE”,“ NY”,“ SUFFOLK”,“ UNIQUE”

-Data is separated by commas so I'm using a delimiter for that. -数据用逗号分隔,因此我为此使用了定界符。

And this is how I'm trying to parse the double values: 这就是我试图解析double值的方式:

 String latitude = scan.next();
 tempLatitude = Double.parseDouble(latitude);
 String longitude = scan.next();
 tempLongitude = Double.parseDouble(longitude);

And I have a catch for Number Format Exception to print the message and I get: 我有一个数字格式异常的陷阱来打印消息,我得到:

For input string: ""+40.922326"" 对于输入字符串:“” +40.922326“”

And the error message(without the catch for NFE): 和错误消息(没有抓住NFE):

Exception in thread "main" java.lang.NumberFormatException: For input string: ""+40.922326""
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at Processor.readData(Processor.java:44)
at Application.main(Application.java:12)

Processor is the class all this stuff is in. Application invokes it through an object the void method readData and I have another class that stores the variables into arrays of that type. Processor是所有这些东西所在的类。应用程序通过对象的void方法readData调用它,而我还有另一个类将变量存储到该类型的数组中。

Let me know if you need to see more code! 让我知道您是否需要查看更多代码! Thanks. 谢谢。

As you can see "+40.922326" has double quotes arround the number. 如您所见, "+40.922326"在数字"+40.922326"带有双引号。 And that can not be parsed. 那是无法解析的。 you have to remove the quotes bevor you parse the number. 您必须删除引号,然后再解析数字。

You need to remove the quotes at the start and the end of the numbers. 您需要删除数字开头和结尾的引号。 You can write a simple method to do that: 您可以编写一个简单的方法来做到这一点:

private double parseDouble(String string) {
    if(string.startsWith("\"")) {
        string = string.substring(1);
    }
    if(string.endsWith("\"")) {
        string = string.substring(0, string.length()-1);
    }
    return Double.parseDouble(string);
}

You can also rely on regular expressions to parse the value wrapped in quotes: 您还可以依靠正则表达式来分析用引号引起来的值:

private double parseDouble(String string) {
    Pattern pattern = Pattern.compile("\"?([+\\-]?\\d+(\\.\\d+)?)\"?");
    Matcher matcher = pattern.matcher(string);
    if (matcher.find()) {
        return Double.parseDouble(matcher.group(1));
    }
    return 0;
}

System.out.println(parseDouble("\"+40.922326\""));  // prints 40.922326
System.out.println(parseDouble("\"-40.922326\""));  // prints -40.922326

您也可以使用substring()方法删除双引号。

tempLatitude = Double.parseDouble(latitude.substring(1,latitude.length()-1);

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

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