【问题标题】:Java string producing error when converting to an int转换为 int 时 Java 字符串产生错误
【发布时间】:2017-03-05 05:44:40
【问题描述】:

我正在尝试使用 Integer.parseInt() 将表示电话号码的字符串转换为我可以使用的 int。

这是我得到的错误:

Enter a 10-digit telephone number with optional space or dash after the first and the second block of three digits >
5558759142
Processing encrypted number "5558759142"
Exception in thread "main" java.lang.NumberFormatException: For input string: "5558759142"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)
    at TelephoneNumberValidator.main(TelephoneNumberValidator.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

这是我的代码:

import java.util.*;
import java.lang.Integer;

public class TelephoneNumberValidator {
    public static void main(String[] args) {
        final String VALID_PHONE_PATTERN = "[0-9]{3}[-| ]?[0-9]{3}[-| ]?[0-9]{4}";
        final int NUMBERS_FOLLOWING_AREA_CODE = 7;
        final int DECRYPTED_AREA_CODE = 212;
        Scanner scanKeyboard = new Scanner(System.in);
        String userInput;

        do {
            System.out.println( "Enter a 10-digit telephone number with optional space or dash after the first and the second block of three digits >" );
            userInput = scanKeyboard.nextLine();
            //check to see if the phone number is correct
            if ( !userInput.matches(VALID_PHONE_PATTERN) )
            {
                System.out.println("The given input \" " + userInput + " \" is not valid");
            }
        }while ( !userInput.matches(VALID_PHONE_PATTERN) );

        //extract int from string
        String phoneJustNumbers = userInput;
        phoneJustNumbers = phoneJustNumbers.replaceAll("-",""); // replaces hyphens
        phoneJustNumbers = phoneJustNumbers.replaceAll(" ", "");

        System.out.println( "Processing encrypted number \"" + phoneJustNumbers + "\"" );
        //check the first 3 digits (aka area code) to see if it matches 212
        int areaCode =  Integer.parseInt(phoneJustNumbers);//gets area code
        int placeholderForAreaCode = 111; //set 111 so that each number in area code can be accounted for
        int placeholderForEntirePhoneNum = 1111111111; //set each digit to 1 so we can shift later
        for (int j = 0; j <= 9; j++)
        {
            if ( (areaCode + ( placeholderForAreaCode * j ) == DECRYPTED_AREA_CODE ) ) // we are shifting each digit by one
                                                                                        // to see if it matches the decrypted area code
            {
                System.out.println("The telephone number is \"" + ( Integer.parseInt(phoneJustNumbers) * placeholderForEntirePhoneNum * j )
                        + "\" with a shift value of " + j);
            }
            else
            {
                System.out.println("The telephone number \"" + userInput + "\" cannot be decrypted area code." );
            }
        }

    }
}

因此,当我尝试仅替换硬编码字符串时,它可以使用 Integer.parseInt()。这让我相信我生成清理字符串的方式以某种方式阻止了它的工作。 我是 java 新手,所以我不知道为什么会这样或者我做错了什么。

【问题讨论】:

    标签: java string parsing int


    【解决方案1】:

    int 的最大值可以是 2,147,483,647。 (请参阅Integer.MAX_VALUE。)您的示例电话号码远大于此。尝试改用Long.parseLong(),因为long 的最大值是9,223,372,036,854,775,807。

    【讨论】:

    • 是的,这就是问题所在。非常感谢!
    【解决方案2】:

    输入超出了整数可以容纳(存储)的最大值。将数据类型更改为 long 以克服此问题。

    似乎太多的整数操作 - 可能创建一个电话类,具有前缀等,作为单独的字段/属性,然后对它们进行操作。除非你需要做一些数学运算,否则使用 String 可能会更好,转换是不可取的

    【讨论】:

      【解决方案3】:

      超出范围。您可以放入 int simple 的最大数字不适合您的数字所代表的 50 亿个值。

      请多尝试。

      或者,更好的是:使用特定的类来表示电话号码,例如通过使用仅包含该号码的数字的数组,但还保留您从用户那里获得的完整初始字符串。

      问题是:电话号码包含数字这一事实并不意味着电话号码应该存储为真实号码!

      【讨论】:

      • 是的,这就是问题所在。非常感谢!
      • 那就继续前进,接受您认为对您有帮助的答案。
      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 2014-01-25
      • 2013-11-03
      • 2015-11-26
      • 1970-01-01
      • 2019-02-16
      • 2011-09-15
      • 2012-06-28
      相关资源
      最近更新 更多