Best implementation for an isNumber(string) method

前端 未结 19 2368
感动是毒
感动是毒 2020-12-15 20:04

In my limited experience, I\'ve been on several projects that have had some sort of string utility class with methods to determine if a given string is a number. The idea h

相关标签:
19条回答
  • 2020-12-15 20:31

    I needed to refactor code like yours to get rid of NumberFormatException. The refactored Code:

    public static Integer parseInteger(final String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        final Scanner sc = new Scanner(str);
        return Integer.valueOf(sc.nextInt());
    }
    

    As a Java 1.4 guy, I didn't know about java.util.Scanner. I found this interesting article:

    http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Java

    I personaly liked the solution with the scanner, very compact and still readable.

    0 讨论(0)
  • 2020-12-15 20:31
    public static boolean CheckString(String myString) {
    
    char[] digits;
    
        digits = myString.toCharArray();
        for (char div : digits) {// for each element div of type char in the digits collection (digits is a collection containing div elements).
            try {
                Double.parseDouble(myString);
                System.out.println("All are numbers");
                return true;
            } catch (NumberFormatException e) {
    
                if (Character.isDigit(div)) {
                    System.out.println("Not all are chars");
    
                    return false;
                }
            }
        }
    
        System.out.println("All are chars");
        return true;
    }
    
    0 讨论(0)
  • 2020-12-15 20:32

    A modified version of my previous answer:

    public static boolean isInteger(String in)
    {
        if (in != null)
        {
            char c;
            int i = 0;
            int l = in.length();
            if (l > 0 && in.charAt(0) == '-')
            {
                i = 1;
            }
            if (l > i)
            {
                for (; i < l; i++)
                {
                    c = in.charAt(i);
                    if (c < '0' || c > '9')
                        return false;
                }
                return true;
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-15 20:33

    That's my implementation to check whether a string is made of digits:

    public static boolean isNumeric(String string)
    {
        if (string == null)
        {
            throw new NullPointerException("The string must not be null!");
        }
        final int len = string.length();
        if (len == 0)
        {
            return false;
        }
        for (int i = 0; i < len; ++i)
        {
            if (!Character.isDigit(string.charAt(i)))
            {
                return false;
            }
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-12-15 20:34

    Here is our way of doing this:

    public boolean isNumeric(String string) throws IllegalArgumentException
    {
       boolean isnumeric = false;
    
       if (string != null && !string.equals(""))
       {
          isnumeric = true;
          char chars[] = string.toCharArray();
    
          for(int d = 0; d < chars.length; d++)
          {
             isnumeric &= Character.isDigit(chars[d]);
    
             if(!isnumeric)
             break;
          }
       }
       return isnumeric;
    }
    
    0 讨论(0)
  • 2020-12-15 20:34

    For long numbers use this: (JAVA)

    public static boolean isNumber(String string) {
        try {
            Long.parseLong(string);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题