ReplaceAll not working

2.1k views Asked by At

I'm using google volley to retrieve source code from website. Some looping was done to capture the value in the code. I've successfully captured the data I wanted, but error was shown: NumberFormatException: Invalid float: "2,459.00"

My intention was to store the value after the class=ListPrice> Sample: RM 2,899.00

The example value of the source code I wanted to save is "RM2,459.00 "

Below is the code I've written:

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);
        }
    };
}

The problem was as below:

I've tried all sort of replaceAll(),

1)replaceAll("[^\d,]+","") result:2,89900

replace all character except digits and comma works.

2)replaceAll("[^\d]+","") result:Invalid int""

replace all character include comma and dot ,not working

3)replaceAll("[^\d.]+,"") result:Invalid int""

replace all character exclude digits and dot, not working

From the experiment 2&3 coding above,I've noticed that if the comma were removed,i cant parseFloat as the value received by it is: "".NumberFormatException:Invalid Float:"" shown.

From the experiment 1,NumberFormatException:Invalid Float "2,45900" is showned.

The problem was replacing comma ,the code will not work but with the presence of comma ,the value cannot be stored into string

5

There are 5 answers

1
N Dorigatti On

Try to parse the number by specifying the Locale.

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

I'm just guessing the locale, don't know what you should use, depends on country

5
N Dorigatti On

the problem is that in your code:

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

You are replacing coma but not storing the value! you have to do:

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

I'm not sure of the pattern "\D+\s" because if you remove the coma you don't need to replace anything else (except "RM" that i assume you already removed)

Update: set locale and parse a number:

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

You need to do it one by one

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

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

now

 priceString=priceString.trim();
 float price = Float.parseFloat(priceString);
2
Amarjit On

use `replaceAll(Pattern.quote(","), "");

EDIT

if you want only numbers then use this

String s1= s.replaceAll("\D+","");

2
Benjamin Guino On

try this:

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