简体   繁体   English

输入字符串999999999的NumberFormatException

[英]NumberFormatexception for input string 999999999

this is my html code 这是我的html代码

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

this is my controller 这是我的controller

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

I am gettting error of NumberFormatException for input string '9999999999' 我正在NumberFormatException for input string '9999999999'NumberFormatException for input string '9999999999'错误

How to solve it. 如何解决。

Even though it is a number why cannot I parse it? 即使是数字,我为什么也不能解析它?

9999999999 is outside the valid range of values for the int data type (-2 31 to 2 31 -1, inclusive), as specified by the Integer.MIN_VALUE and Integer.MAX_VALUE constants. 9999999999超出了由Integer.MIN_VALUEInteger.MAX_VALUE常量指定的int数据类型的有效值范围(-2 31到2 31 -1,包括)。

You cannot represent a full phone number in an int , you would have to omit the prefix and area code (0000000 - 9999999). 您不能在int表示完整的电话号码,而必须省略前缀和区号(0000000-9999999)。 Otherwise, use a long instead (-2 63 to 2 63 -1, inclusive), Long.parseLong() will happily handle 9999999999 . 否则,请改用long (-2 63到2 63 -1,包括2和1), Long.parseLong()将很高兴地处理9999999999

The Exception because you are trying to convert '9999999999' into an Integer, and the max range of type int is 2147483647 . Exception因为您试图将'9999999999'转换为Integer,并且int类型的最大范围是2147483647

So try Long.parseLong("9999999999") instead if you are insisting on converting phone number from String into numbers. 因此,如果您坚持Long.parseLong("9999999999")电话号码从String转换为数字,请尝试使用Long.parseLong("9999999999") Storing and manipulating phone numbers as int or long will result in some inconsistencies in the future. 将电话号码存储和处理为intlong会导致将来出现一些不一致的情况。

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: 这种方式非常有用,因为您可以检查格式,分隔符等。请从MKyoung网站上查看以下示例:

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. 祝好运。

The range of an int in Java is -2,147,483,648 to 2,147,483,647 . Java中int的范围是-2,147,483,6482,147,483,647

9,999,999,999 is out of range and that is what is causing the exception. 9,999,999,999超出范围,这就是导致异常的原因。

Check parseInt() method in Oracles doc parseInt 检查Oracle文档parseInt中的 parseInt()方法

It clearly says 它清楚地说

An exception of type NumberFormatException is thrown if any of the following situations occurs: 如果发生以下任一情况,将引发NumberFormatException类型的异常:

The first argument is null or is a string of length zero. 第一个参数为null或长度为零的字符串。 The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX. 基数小于Character.MIN_RADIX或大于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 '-' ('\-') or plus sign '+' ('\+') provided that the string is longer than length 1. The value represented by the string is not a value of type int. 字符串的任何字符都不是指定基数的数字,除非第一个字符可以是减号'-'('\\ u002D')或加号'+'('\\ u002B')(前提是该字符串是大于长度1。字符串表示的值不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM