开发者

Parse strange number from Google currency convertor in Java

I am trying to parse values are returned from this url. The problem is that a receive 27 458 number开发者_JAVA百科 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?


What about if you remove the space first?

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

And then parse it?


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜