0

I'm trying to build a hashMap from an ArrayList which contains all the variables I need plus their respective values.

The problem is, my arrayList contains variables with non numeric values (eg: var1 = "*$&/@"). How could I filter the data contained in the arrayList to get only the numeric strings.

I tried using regular expressions but the data get filtered too much and some of the variables I need get lost. I guess i'm not using the legit regex. So I tried matching the following regex and if not, assign "0" to my variable. Here's roughly what I've tried thus far:

       private static final String REGEX = "-?\\d+(\\.\\d+)?";

       //...

       if (val_ens1_sol.matches(REGEX) && val_ens1_bord.matches(REGEX)) {

                 reslutatsMap.put(key_ens1_sol, val_ens1_sol);
                 reslutatsMap.put(key_ens1_bord, val_ens1_bord);

              } else {
                 val_ens1_sol = "0";
                 val_ens1_sol = "0";

              }

2 Answers2

1

This was already answered somewhere else (How to check if a String is numeric in Java) but to discuss the possibilities: Either you assume that you have numeric strings, parse the string is integer or double and catch the number format exception, or you use a regex.

Community
  • 1
  • 1
Smutje
  • 17,733
  • 4
  • 24
  • 41
0

You can do this using BigDecimal, which will parse all integers/decimal point floats/scientific floats:

try {
    new BigDecimal(val_ens1_sol);
    new BigDecimal(val_ens1_bord);
} catch (NumberFormatException ignored) {
    // deal with at least one value not being a number
}
fge
  • 119,121
  • 33
  • 254
  • 329
  • **This is evil** An Exception should be- as the name so subtly implies - exceptional and not part of your standard control flow. If the OP actually expects non-numeric values in the input set, then a regex would be the way to go. – Mike Adler Feb 24 '14 at 08:18
  • Sorry, but I disagree. This is a perfectly sensible way to deal with the situation. It is akin to checking whether a string is in a valid date format. – fge Feb 24 '14 at 08:21
  • 1
    No. It is not. It will slow down a program heavily when it will throw a lot of NumberFormatExceptions implying that the array has a lot of non-numbers. – Leo Pflug Feb 24 '14 at 08:32
  • @LeoPflug in this case you have another problem to deal with: the data. And as is shown by the OP's question, writing a proper regex is hard (shall I say Jamie Zawinski?). You don't use a regex to validate IP addresses, do you? No, you use `InetAddress`. Same here. – fge Feb 24 '14 at 08:37
  • First, exception handling is slow. For a large set of input data, especially for many non-numeric entries, this will significantly imact the runtime performance. Second, readability and intent is diminished. The OP's intent is "copy the value into the arraylist if it is numeric", your interpretation is "continue with control flow if the value does not throw a NumberFormatException". Third, you are generating a load of new objects which are then immediately garbage collected - for a given value of immediately. – Mike Adler Feb 24 '14 at 08:42
  • Not saying your answer is invalid. It works. It's just bad form. – Mike Adler Feb 24 '14 at 08:45