【发布时间】:2015-06-25 08:45:48
【问题描述】:
我正在使用 google volley 从网站检索源代码。进行了一些循环以捕获代码中的值。我已经成功捕获到我想要的数据,但是显示错误:NumberFormatException: Invalid float: "2,459.00"
我的意图是存储 class=ListPrice 之后的值> 样本: RM 2,899.00
我要保存的源代码示例值为“RM2,459.00”
下面是我写的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lazada_result);
lelongResult = (TextView) findViewById(R.id.lelong_result);
RequestQueue lelong = MyVolley.getRequestQueue(this);
StringRequest myLel = new StringRequest(
Method.GET,
"http://list.lelong.com.my/Auc/List/List.asp?DA=A&TheKeyword=iphone&x=0&y=0&CategoryID=&PriceLBound=&PriceUBound=",
RetrieveLelong(), createMyReqErrorListener());
lelong.add(myLel);
}
private Response.Listener<String> RetrieveLelong() {
return new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ArrayList<Float> integers = new ArrayList<>();
String to = "class=ListPrice>";
String remainingText = response;
String showP = "";
while (remainingText.indexOf(to) >= 0) {
String tokenString = remainingText.substring(remainingText
.indexOf(to) + to.length());
String priceString = tokenString.substring(0,
tokenString.indexOf("<"));
float price = Float.parseFloat(priceString.replaceAll("[^\\d,]+", "").trim());
integers.add((price / 100));
remainingText = tokenString;
}
for (int i = 0; i < integers.size(); i++) {
String test1 = Float.toString(integers.get(i));
showP += test1 + "\n";
}
lelongResult.setText(showP);
}
};
}
问题如下:
我已经尝试了各种 replaceAll(),
1)replaceAll("[^\d,]+","") 结果:2,89900
替换除数字和逗号以外的所有字符。
2)replaceAll("[^\d]+","") 结果:Invalid int""
替换所有字符,包括逗号和点,不起作用
3)replaceAll("[^\d.]+,"") 结果:Invalid int""
替换所有字符,不包括数字和点,不起作用
从上面的实验 2&3 编码中,我注意到如果逗号被删除,我无法解析浮点,因为它接收到的值是:“”.NumberFormatException:Invalid Float:“”显示。
从实验一开始,出现NumberFormatException:Invalid Float "2,45900"。
问题是替换逗号,代码将不起作用,但逗号的存在,值不能存储到字符串中
【问题讨论】:
-
显示代码 - 你如何使用 replaceAll(",", "")?
-
显示更多代码,您在其中使用了这个“价格”变量。
-
@DroidWormNarendra,@AnandSingh,我已经更新了源代码。谢谢
标签: android replaceall