简体   繁体   中英

unable to replace blank space in String java

bit of a weird one this as I have checked it over multiple times and still cannot see anything wrong with what I am doing.

Anyway, I have this string named score that is a method parameter. The input is "0 - 2" , I would like to parse this string but before I must remove the blank spaces. I've tried using score = score.replaceAll("\\\\s+", "") and score = score.replace(" ", "") however the blank spaces are still not replaced. Underneath this line of code I print out the score and it is still displaying the string as "0 - 2" with the spaces not deleted. Any help would be much appreciated.

Here is the code for the method

public static int parseScore(String score, boolean first){

    score = score.replaceAll("\\s+",""); // remove white space
    System.out.println(score);

    String[] tokens = score.split("-");

    if(first == true){

        return Integer.parseInt(tokens[0]);

    }else{

        return Integer.parseInt(tokens[1]);

    }

}

Error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0 " at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at PredictResult2.parseScore(PredictResult2.java:32) at PredictResult2.main(PredictResult2.java:102)

EDIT:

Forgot to mention that the string that I am using has already been parsed from a downloaded html page. Could that have anything to do with it? maybe different character formatting or something?

Your code is perfectly fine. It works just fine when I tested it, so that's not it. It must be some other odd problem.

But anyways, for what you're trying to achieve maybe trim() would be more efficient and quicker.

    public static int parseScore(String score, boolean first){
    String[] tokens = score.split("-");
    if(first == true){
        return Integer.parseInt(tokens[0].trim());
    }else{
        return Integer.parseInt(tokens[1].trim());

    }
}

Seems to be a case of dealing with non-breaking spaces. Stumbled about these a while ago and wrote myself the following util method. More information is found here:

Why is non-breaking space not a whitespace character in java?

https://en.wikipedia.org/wiki/Non-breaking_space

/**
 * Trims non-breaking whitespace, too.
 */
public static String trim(String value) {
    return value.replaceAll("[\\s\\u00A0]+$", "");
}

Your input must be wrong. Your code works good - check this piece of code:

public static void parseScore(){

    String score = "0 - 2";
    score = score.replaceAll("\\s+","");

    System.out.println(score);

    String[] tokens = score.split("-");

    System.out.println(tokens[0]);
    System.out.println(tokens[1]);


    System.out.println(Integer.parseInt(tokens[0]));
    System.out.println(Integer.parseInt(tokens[1]));
}

public static void main(String[] args) {
    parseScore();
}

If you are not sure about the input I suggest converting the string to char array and printing the ASCII code of each character. Then compare the output with the table here .

char[] array = score.toCharArray();
    for (char c : array)
        System.out.print((int)c + " ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM