1

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?

user3918443
  • 105
  • 1
  • 13
  • Have you stepped through the code in your debugger to see what is happening on each line? – Jim Garrison Oct 09 '16 at 16:54
  • System.out.println(score) returns "0 - 2". And therefore when the line return Integer.parseInt(tokens[0]) is executed it returns a NumberFormatException. I will edit in the actual error – user3918443 Oct 09 '16 at 16:58

3 Answers3

1

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

    }
}
NonameSL
  • 1,405
  • 14
  • 27
1

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]+$", "");
}
ldz
  • 2,217
  • 16
  • 21
0

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 + " ");
Wojtek
  • 1,288
  • 11
  • 16
  • Hi, thanks! Yeah I thought this may be the case. If you look at the edit I just made on the post I believe it may be to do with some unusual character formatting from the html page that i have ripped from the web. – user3918443 Oct 09 '16 at 17:15
  • 0 - 2 is the input. Or atleast that is what I copied from the output of the println function. I'll try the character array thing and get back to you anyway, thanks! – user3918443 Oct 09 '16 at 17:37
  • Here is the output from the char array: 48 160 45 160 50. ohhh okay, so space is actually being formatted as "á". So should I try to replace that character instead? – user3918443 Oct 09 '16 at 17:39
  • Yes, you should remove this strange letter from your string. Try this: http://pastebin.com/FU7j4x8E – Wojtek Oct 09 '16 at 17:54