0

Why I cannot get rid of whitespace before thousands?

I have written such method to check if string can be parse to double:

EDIT: OK, I've updated the method because everyone writes the same answer - this is unusual situation

public static boolean isNumber(String test) {
    // remove whitespaces
    System.out.print("Test is - " + test);
    test = test.replaceAll("\\s", "");
    // test = test.replaceAll("[ \\t]", "");
    // test = test.replaceAll("\\s+", "");
    // test = test.replaceAll(" ", "");
    System.out.print(" - now test is - " + test);
    // match pattern - numbers with decimal delimiter as dot or comma
    String decimalPattern = "([0-9]*)(\\.|\\,)([0-9]*)";
    boolean match = Pattern.matches(decimalPattern, test);
    System.out.println(" - Is this number? ===> " + match);
    return match;
}

And now I'm going insane. Here is some of outputs of my method:

[stdout] Test is - aasdfg - now test is - aasdfg - Is this number? ===> false
[stdout] Test is - aa sd fg - now test is - aasdfg - Is this number? ===> false
[stdout] Test is - 123.50 - now test is - 123.50 - Is this number? ===> true
[stdout] Test is - 123,50 - now test is - 123,50 - Is this number? ===> true
[stdout] Test is - 1 123.50 - now test is - 1 123.50 - Is this number? ===> false

The last line of the output is the strange one!

Advice - test value comes from HSSFCell#getStringCellValue() - maybe here is problem. None of commented String#replaceAll works.

robson
  • 1,623
  • 8
  • 28
  • 43
  • your code should work..where is your `test` variable declared..is it being accessed by other thread – Anirudha Jun 27 '13 at 09:20
  • 1
    I would simplify everything. 1) change your program so that it doesn't get the data from a spreadsheet but the "1 123.50" is hardcoded. Does it work?. If it does 2) read the value from a spreadsheet, but one that only contains that single value and has no spaces around it, so you don't the replace stuff, just the matches. Does it work? If so 3) now have the single value in the spreadsheet, but with spaces that need stripping...etc. Keep making it more complex until you reach the point it breaks. It does sound like either another thread is changing the value, or there's an encoding issue... – matt freake Jun 27 '13 at 10:22
  • @Disco 3 1) - hardcoded works - I'm aware of this and tested 2) without space before thousands (so variables less than 1000) works - so 3) it is not thread, but maybe encoding - please look at Peter Crotty's answer – robson Jun 27 '13 at 10:28

8 Answers8

3

Your code is working for me if I type in 1 123

Why not find out what character is in your String?

    for (char c : test.toCharArray())
    {
        System.out.println(0+c);
    }
Peter Crotty
  • 301
  • 2
  • 9
  • Threading is not the issue as the variable is declared locally (String **test**). It is also being reassigned anyway. – Peter Crotty Jun 27 '13 at 09:53
  • Good point! your loop for variable 2 670,99 gave result: 160 54 55 48 44 57 57. What does it mean? first two characters as 160? – robson Jun 27 '13 at 10:21
  • 2
    The 160 is a non-breaking-space. See - http://stackoverflow.com/questions/1822772/java-regular-expression-to-match-all-whitespace-characters . The regular expression for whitespace will not match that, so you'll need to stop the spreadsheet including them, or expand the regexp to remove them. There's some suggestions in that thread like "\p{javaWhitespace}" – matt freake Jun 27 '13 at 10:42
  • @Disco3 Yes, you're right - this also works for me - [\p{Z}\s] does the job also – robson Jun 27 '13 at 10:52
1

Since the whitespace(s) before thousands is(are) "strange" whitespace(s), try this:

test = test.replaceAll("(\\d+)[^\\d.,]+(\\d+)|\\s+", "$1$2");

Matches any character - that are not a digit, a dot or a comma - between two digits

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329
0

The usual way of removing whitespace is:

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

And the comma is not a valid character of a parseable double. Actually, not all numeric combinations are parseable either, due to the maximum and minimum possible values that a double can represent.

The simplest and best way to determine if a string can be parsed as a double is to attempt to parse is using the JDK:

public static boolean isNumber(String test) {{
    try {
        Double.parseDouble(test.trim());
        return true;
    } catch (NumberFormatException ignore) {
        return false;
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0
String s = stemp.replaceAll("\\s","");
shreyansh jogi
  • 2,082
  • 12
  • 20
0

As all said try with \\s and here is my simple way to check :

public static boolean isStringisDoubleOrNot(String myvalue) {
    try {
        Double.parseDouble(myvalue);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Unfortunatelly, when value 123.50 is put to your method - it is ok, but when 1 123.50 - then it returns false. Please notice that I really don't know what charactes is between thousand and hundreds – robson Jun 27 '13 at 09:39
0

what about such an approach to check if string can be parsed to double?

static boolean canBeParsed(String str)
{
    try
    {
        Double.parseDouble(str.trim().replaceAll("\\s",""));
    }
    catch (Exception ignored)
    {
        return false;
    }
    return true;
}

edit: if you want to remove whitespaces also from within the string, use .replaceAll("\s","")

wojtuch
  • 188
  • 2
  • 11
0

I guess your variable test is being accessed by other thread.

Use synchronized keyword

synchronized(test)
{
     //your complete code
}
Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

Let's say that we don't know what exactly is before thousand. It could be \n, \t, ' ' or dunno what. So instead of removing only whitespace try to pass only numbers and dot, with sinple "for loop" and ignore everything else.

StringBuilder sb;
for (int i = 0; i < test.length;  ++i)
     if ((test.charAt(i) >= '0'  &&  test.charAt(i) <= '9')  ||  test.charAt(i) == '.')
        sb.append(test.charAt(i));
Kamen Stoykov
  • 1,715
  • 3
  • 17
  • 31