NumberFormatexception for input string 999999999

2019-08-26 19:39发布

this is my html code

<div class="form-group col-md-6">
    <input type="text" class="form-control" name="phonenumber" placeholder="Enter Phone Number">
</div>

this is my controller

int phonenumber=Integer.parseInt(request.getParameter("phonenumber").trim());

I am gettting error of NumberFormatException for input string '9999999999'

How to solve it.

Even though it is a number why cannot I parse it?

标签: java parsing
4条回答
走好不送
2楼-- · 2019-08-26 19:42

The Exception because you are trying to convert '9999999999' into an Integer, and the max range of type int is 2147483647.

So try Long.parseLong("9999999999") instead if you are insisting on converting phone number from String into numbers. Storing and manipulating phone numbers as int or long will result in some inconsistencies in the future.

If you are doing that to check whether all the input characters are digits or not, you can use other ways such as using Regular Expressions. This way is very helpful since you can check formats, separator, etc. See this sample from MKyoung site:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidatePhoneNumber {
  public static void main(String[] argv) {

      String sPhoneNumber = "605-8889999";
      //String sPhoneNumber = "605-88899991";
      //String sPhoneNumber = "605-888999A";

      Pattern pattern = Pattern.compile("\\d{3}-\\d{7}");
      Matcher matcher = pattern.matcher(sPhoneNumber);

      if (matcher.matches()) {
          System.out.println("Phone Number Valid");
      }
      else
      {
          System.out.println("Phone Number must be in the form XXX-XXXXXXX");
      }
 }
}

And another simple way is to have a method which checks all the digits of a phone number are really digits:

public boolean isAllCharactersDigit(String phoneNumber){
    for(char c: phoneNumber.toCharArray()){
        if(!Character.isDigit(c))
            return false;
    }
    return true;
}

Good Luck.

查看更多
再贱就再见
3楼-- · 2019-08-26 19:47

9999999999 is outside the valid range of values for the int data type (-231 to 231-1, inclusive), as specified by the Integer.MIN_VALUE and Integer.MAX_VALUE constants.

You cannot represent a full phone number in an int, you would have to omit the prefix and area code (0000000 - 9999999). Otherwise, use a long instead (-263 to 263-1, inclusive), Long.parseLong() will happily handle 9999999999.

查看更多
混吃等死
4楼-- · 2019-08-26 19:59

Check parseInt() method in Oracles doc parseInt

It clearly says

An exception of type NumberFormatException is thrown if any of the following situations occurs:

The first argument is null or is a string of length zero. The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.

Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') or plus sign '+' ('\u002B') provided that the string is longer than length 1. The value represented by the string is not a value of type int.

Examples:

 parseInt("0", 10) returns 0
 parseInt("473", 10) returns 473
 parseInt("+42", 10) returns 42
 parseInt("-0", 10) returns 0
 parseInt("-FF", 16) returns -255
 parseInt("1100110", 2) returns 102
 parseInt("2147483647", 10) returns 2147483647
 parseInt("-2147483648", 10) returns -2147483648
 parseInt("2147483648", 10) throws a NumberFormatException
 parseInt("99", 8) throws a NumberFormatException
 parseInt("Kona", 10) throws a NumberFormatException
 parseInt("Kona", 27) returns 411787
查看更多
太酷不给撩
5楼-- · 2019-08-26 20:04

The range of an int in Java is -2,147,483,648 to 2,147,483,647.

9,999,999,999 is out of range and that is what is causing the exception.

查看更多
登录 后发表回答