![](/img/trans.png)
[英]getting wrong output while using numberFormat.parse(“”) method in java
[英]Getting NumberFormat while trying to parse a string
我正在嘗試將字符串解析為整數。 當我執行sys out時,該字符串是一個數字,但是當我嘗試解析它時,它會在開頭添加雙引號。 我沒有做任何其他事情。
以下是我的代碼片段:
System.out.println("Result 2 is: "+results[2]);
String temp = results[2].replace("\"", "");
System.out.println("String Temp is: "+temp);
int height = Integer.parseInt(results[1].replace("\"", ""));
int width = Integer.parseInt(results[2].replace("\"", ""));
在日志中:
字符串溫度為:2550
wt.system.err wcadmin-java.lang.NumberFormatException:對於輸入字符串:“ 2550
此異常僅適用於result [2]。 與字符集有關?
選項1:解析temp
。 更改
int width = Integer.parseInt(results[2].replace("\"", ""));
至
int width = Integer.parseInt(temp);
選項2:使用正則表達式刪除所有非數字內容,而不是嘗試手動處理極端情況。
int width = Integer.parseInt(results[2].replaceAll("\\D+", ""));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.