0

I am uisng the folowing logic to check if a String recievied is a valid number or not

package com;


public class Test {
    public static void main(String args[]) {
        String str = "122";
        boolean b = isNumb(str);
        System.out.println(b);
    }

    public static boolean isNumb(String str) {
        String s = str;
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isDigit(s.charAt(i)))
                return false;
        }
        return true;
    }

}

I am going to use this in a highly multithreaded environment where there can be 800 - 900 concurrent users at a time , please let me know , if this code has got any loop holes or not ??

Please share your views

Thanks in advance

  • 2
    Can you use something like this? http://stackoverflow.com/questions/1102891/how-to-check-a-string-is-a-numeric-type-in-java – VM4 Aug 06 '13 at 11:14
  • Btw, your code won't work with floating point numbers, not even talking about hex and stuff. – VM4 Aug 06 '13 at 11:15
  • Your approach is completely okay. Another alternative is to use `str.toCharArray()` and directly check on the resulting `char[]` to not check bounds in the `charAt()` method. But I don't think you'll notice the performance improvement for small strings (especially vs. the garbage char[] generated that needs to be collected). – Thomas Jungblut Aug 06 '13 at 11:15
  • Your code will break on negative numbers :) – Matthias Aug 06 '13 at 11:16
  • @anubhava Overkill? :p – keyser Aug 06 '13 at 11:17
  • `String s = str` is of no use in your code. – assylias Aug 06 '13 at 11:23
  • By the way you should store s.length in a variable and use that variable in the for loop. This way the length of the string will be calculated only once. (Will be noticeable for longer strings) – giorashc Aug 06 '13 at 12:08

4 Answers4

5

I would use regex:

public static boolean isNumb(String str) {
    return str.matches("\\d+");
}

To return true for negative numbers too, add an optional leading dash:

return str.matches("-?\\d+");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

There are better ways to check if a string is a number, such as using a regular expression.

s.matches("^-?\\d+(\\.\\d)?$")

will easily pick up whether the string is number, where s is your string.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
Matthew Tyler
  • 316
  • 2
  • 7
2

For validating if given string is a valid number (not just Integer):

boolean b = str.matches("^[+-]?(?=.)\\d*(\\.\\d+)?$");
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

I would simply do the following to check if a String is a number:

try {
    final Integer i = Integer.parseInt("Your String");
} catch(final NumberFormatException nfe) {
    //String is no number
}
Holger
  • 496
  • 3
  • 8
  • 1
    This has a performance issue when the check fails. It is an example of the "using exceptions for flow control" anti-pattern. Still, it *is* a reliable approach. – Marko Topolnik Aug 06 '13 at 11:21
  • @MarkoTopolnik If it is expected that most inputs will be numbers, this is a good compromise (performance is as good as loop on char when no exception is thrown). – assylias Aug 06 '13 at 11:25
  • 1
    @assylias Personally, I rarely ever bother using anything else, mainly due to the reliability aspect. There is always something that may be wrong with my regex, or any other complex logic I may have. In fact, I'm a bit mad at the JDK for not providing a non-throwing method itself. – Marko Topolnik Aug 06 '13 at 11:26