1

I am trying to parse values are returned from this url. The problem is that a receive 27 458 number with space inside and I can't remove that space! Float.parseFloat also declines this number because of this space. Do somebody have a solution for this?

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
nKognito
  • 6,297
  • 17
  • 77
  • 138

4 Answers4

3

What about if you remove the space first?

String resultString = subjectString.replaceAll("\\s", "");

And then parse it?

FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • @nKognito If you remove all spaces how do you have a space in your number? :O – FailedDev Oct 17 '11 at 09:37
  • Not to dig up the dead but I just ran into this. This is because the space is not actually a space. With a hex viewer you will note that it is actually '002000'. Also the above code marked as the answer will NOT Parse the string properly if lets say you receive "1 1123.3445" – Mike McMahon Mar 07 '12 at 23:35
  • @MikeMcMahon then what is the solution?? how do we remove the special character � from the response? – Naveen Oct 12 '12 at 10:11
  • @Peter see my solution below, it's a slight variant from the one above (which didn't work for me at all btw) ! (i don't break when we find the `.` because we need the digits after) – Mike McMahon Oct 12 '12 at 18:30
  • 1
    Thanks @MikeMcMahon.. But I used this one line of code to replace the special character �.. result = result.replaceAll("\uFFFD", ","); – Naveen Oct 12 '12 at 18:46
0

This will add the digits as they are encountered, if it is not a digit it will test to determine if it is a . mark. If it is it will append it, otherwise skip and keep adding digits until there are no more digits to add.

private String correct(String src) {
    String tag = "";
    for (int i = 0; i < src.length(); i++) {
        char c = src.charAt(i);
        if (Character.isDigit(c)) {
            tag += c;
        } else if (c == '.'){
            tag += '.';
        }
    }
    return tag;
}
Mike McMahon
  • 7,096
  • 3
  • 30
  • 42
0

Get it as a string and then do a replaceAll exchanging the whitespace for nothing. I could paste the code but I'd be copying it from here: Removing whitespace from strings in Java

Community
  • 1
  • 1
mikey
  • 2,022
  • 1
  • 13
  • 6
-1

Please use following code to adjust your string:

private String correct(String src) {
    boolean found = false;
    String tag = "";
    for (int i = 0; i < src.length(); i++) {
        char c = src.charAt(i);
        if (Character.isDigit(c)) {
            tag += c;
        } else {
            if (found) {
                break;
            } else {
                found = true;
                tag += ".";
            }
        }
    }
    return tag;
}
Tran Dinh Thoai
  • 712
  • 1
  • 7
  • 21
  • This never makes it the full length of the string (if it encounters the decimal point early and or any other non-digit characters) and only retrieves one digit after the decimal point... – Mike McMahon Oct 12 '12 at 18:32