简体   繁体   English

使用Java Double.parseDouble和Android中的相同方法有区别吗?

[英]Is there a difference when using Java Double.parseDouble and the same method in Android?

The reason I ask is because I have 2 exact same bits of code in an android app and in a java program doing the same thing (collecting tidal data): 我问的原因是因为我在Android应用程序和Java程序中做相同的事情(收集潮汐数据)时有2个完全相同的代码位:

String[] string4 = tide4.replaceAll("[LmH]", "").split(" ");
         Time4 = string4[0];
         String height4 = string4[1].replaceAll("\\s", "");

         System.out.println("HEIGHT4 = " + "'" + height4 + "'" );

        //Convert the Meters into feet for tide heights
      double heightM4 = Double.parseDouble(height4); //FAILS ON THIS LINE IN **JAVA** NOT IN ANDROID

Exception: java.lang.NumberFormatException: For input string: "6.24 " 异常: java.lang.NumberFormatException: For input string: "6.24 "

However in the java program running on my machine it fails when parsing it? 但是,在我的机器上运行的Java程序中,解析时失败了吗? in Android it carries on fine. 在Android中,它运行良好。 I have noticed one thing different when testing. 我注意到测试时有一点不同。 When printing the value of "height4" in eclipse on my machine it outputs something like this: HEIGHT4 = '6.24 ' <--Notice the space! 当在我的机器上以eclipse打印“ height4”的值时,它会输出如下内容: HEIGHT4 = '6.24 ' <--Notice the space! In Android it outputs this: HEIGHT4 = '6.24'<--No Space? 在Android中,它输出以下内容: HEIGHT4 = '6.24'<--No Space? Nothing is different between the 2 bits of code except the platform they are running on( as far as I can see). 除了它们运行的​​平台(据我所知)之外,这两位代码之间没有什么不同。 What could the cause of this be? 这可能是什么原因? Something to do with that un-removable whitespace? 与该不可移动的空白有关?

What could the cause of this be? 这可能是什么原因? Something to do with that un-removable whitespace? 与该不可移动的空白有关?

I think it is everything to do with that mysterious character. 我认为这与这个神秘角色有关。 It is clearly not a digit or one of the other characters that are valid in a number, and that would definitely result in a parse error. 显然不是数字或数字中有效的其他字符之一,这肯定会导致解析错误。

What you need to do is to use a debugger or some trace printing to find out what the mysterious character is. 您需要做的是使用调试器或一些跟踪打印来找出神秘字符。 For example: 例如:

    char last = height4.charAt(height4.length() - 1); 
    System.out.println("The last character codepoint is " + ((int) last));

then convert the codepoint to hexadecimal and look it up in the Unicode tables at http://unicode.org 然后将代码点转换为十六进制并在http://unicode.org的Unicode表中查找

Once you have done that, you can figure out where the mystery is coming from and / or what is the best way to get rid of it. 完成此操作后,您可以弄清楚谜团的来历和/或消除谜团的最佳方法是什么。


Advice: If I would you, I would focus on figuring out where the character is coming from. 忠告:如果我愿意,我将专注于弄清楚角色来自何处。 The fact that you are getting strange stuff is a worry, and points to the potential for other problems. 您正在获取奇怪的东西的事实令人担忧,并指出了其他问题的可能性。 If you just try to get rid of the bad stuff, then you may be sweeping a larger problem under the carpet; 如果您只是想摆脱不良的东西,那么您可能会在地毯下扫除更大的问题。 eg making it harder to find. 例如,使其更难找到。


UPDATE - The 0x00A0 code point is the "non-breaking space" (NBSP) character in the LATIN-1 supplement: 更新 0x00A0代码点是LATIN-1补充中的“不间断空格”(NBSP)字符:

  • The String.trim() method doesn't remove this. String.trim()方法不会删除它。 It only removes ASCII control codes; 它仅删除ASCII控制代码; ie 0x0000 through 0x0020 . 0x00000x0020

  • In the regex language implemented by Pattern , "\\\\s" doesn't match all Unicode space variants. Pattern实现的正则表达式语言中, "\\\\s"与所有Unicode空间变体都不匹配。 This Question has the solution: Java regular expression to match _all_ whitespace characters 这个问题有解决方案: Java正则表达式匹配_all_个空格字符

The presence of a NBSP is not as problematic as "random characters", but it suggests that you are "scraping" your input from rendered text (eg HTML) and there are differences in the rendering or scraping processes between the two platforms. NBSP的存在不像“随机字符”那样麻烦,但是它表明您正在“渲染”来自渲染文本(例如HTML)的输入,并且两个平台之间的渲染或抓取过程有所不同。 While not worrying per se , this is something you need to keep in mind. 虽然本身不必担心,但您需要牢记这一点。

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

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