【问题标题】:ReplaceAll not workingReplaceAll 不起作用
【发布时间】: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


【解决方案1】:

试试这个:

float price = Float.parseFloat(priceString.replaceAll("RM", "").trim());

【讨论】:

  • RM 是货币...我正在从在线购物网站检索源代码。
  • @CodeX,我试过你的代码,但错误仍然存​​在。NumberFormatException: Invalid int: "2,459.00"
【解决方案2】:

使用`replaceAll(Pattern.quote(","), "");

编辑

如果你只想要数字然后使用这个

字符串 s1= s.replaceAll("\D+","");

【讨论】:

  • ,错误仍然存​​在。 NumberFormatException:无效的 int:“2,459.00”
  • 检查我发布的更新问题,我发现逗号替换有问题
【解决方案3】:

尝试通过指定区域设置来解析数字。

NumberFormat format = NumberFormat.getInstance(Locale.KOREAN);
Number number = format.parse(priceString.replaceAll("RM", ""));
double d = number.doubleValue();

我只是猜测语言环境,不知道你应该使用什么,取决于国家/地区

【讨论】:

  • 我已经更新了问题,似乎问题在于逗号替换。 :)
【解决方案4】:

你需要一件一件的去做

 priceString=priceString.replaceAll("\\D", "");

 priceString=priceString.replaceAll("\\s", "");

现在

 priceString=priceString.trim();
 float price = Float.parseFloat(priceString);

【讨论】:

  • 已根据您的代码进行了更改,但无效的浮动显示:“”,一直在测试不同的正则表达式的批次,发现有关逗号的问题...每当替换或删除逗号时,错误显示,否则没有问题。
【解决方案5】:

问题在于您的代码中:

priceString.replaceAll(Pattern.quote(","), "");
float price = Float.parseFloat(priceString.replaceAll("\\D+\\s+", "").trim());  

您正在替换 coma 但不存储值! 你必须这样做:

priceString = priceString.replaceAll(",", "");
float price = Float.parseFloat(priceString.replaceAll("\\D+\\s+", "").trim());    

我不确定 "\D+\s" 模式,因为如果你删除昏迷,你不需要替换任何其他东西(除了我假设你已经删除的 "RM")

更新:设置语言环境并解析数字:

   NumberFormat format = NumberFormat.getInstance(Locale.KOREAN); 
Number number = format.parse(priceString.replaceAll("RM", "")); 
double d = number.doubleValue();

【讨论】:

  • 我尝试更改这些:replaceAll("[^\d,]+","") 结果:2,899.00 replaceAll("[^\d]+","") 结果:无效int"" replaceAll("[^\d.]+,"") result:Invalid int"" 但我无法得到结果...
  • 请澄清一下,这个问题和进一步的修改无助于理解你拥有什么,你做什么以及你想要什么......
  • @N Dorigatti,对于令人困惑和混乱的问题,我们深表歉意。我试图用更清晰的解释来更新我的问题。谢谢。
  • 据我了解,您收到“RM2,899.00”作为字符串,并且您想将值 2899.00 存储在数据库中(我使用“.”作为小数分隔符),对?如果是这样,您可以执行 replaceAll("RM","") 删除字母数字字符,然后执行 replaceAll(",","") 删除逗号并将数字 2899.00 作为字符串(您也可以进行修剪() 删除空格);此时您可以将字符串解析为浮点数或双精度数,但您应该设置一个语言环境,因为我不知道您的国家/地区默认值! ____ 如果这不能回答您的问题,请将输入和所需输出写为评论
  • @N Dorigatti,感谢您提供如此详细的解释。如何设置区域设置以便系统识别?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多