Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换为int时产生Java字符串错误_Java_String_Parsing_Int - Fatal编程技术网

转换为int时产生Java字符串错误

转换为int时产生Java字符串错误,java,string,parsing,int,Java,String,Parsing,Int,我试图使用Integer.parseInt()将表示电话号码的字符串转换为我可以使用的整数 这就是我得到的错误: 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 "

我试图使用Integer.parseInt()将表示电话号码的字符串转换为我可以使用的整数

这就是我得到的错误:

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." );
            }
        }

    }
}
import java.util.*;
导入java.lang.Integer;
公用电话号码记录器{
公共静态void main(字符串[]args){
最后一个字符串有效_PHONE_PATTERN=“[0-9]{3}[-|]?[0-9]{3}[-|]?[0-9]{4}”;
区域代码后的最终整数=7;
最终整数解密的_区域_代码=212;
扫描仪扫描键盘=新扫描仪(System.in);
字符串用户输入;
做{
System.out.println(“输入一个10位数的电话号码,在三位数的第一个和第二个数据块后面加空格或破折号>”;
userInput=scanKeyboard.nextLine();
//检查电话号码是否正确
如果(!userInput.matches(有效的电话模式))
{
System.out.println(“给定的输入\”+userInput+“\”无效”);
}
}而(!userInput.matches(有效的电话模式));
//从字符串中提取int
字符串phoneJustNumbers=userInput;
phoneJustNumbers=phoneJustNumbers.replaceAll(“-”,“”);//替换连字符
phoneJustNumbers=phoneJustNumbers.replaceAll(“,”);
System.out.println(“处理加密号码\”+phoneJustNumbers+“\”);
//检查前3位数字(又名区号)是否匹配212
int areaCode=Integer.parseInt(phoneJustNumbers);//获取区号
int PLACEHOLDERFOREACODE=111;//设置111以便可以计算区号中的每个数字
int placeholder forentirephonenum=1111111;//将每个数字设置为1,以便稍后进行移位

对于(int j=0;j超出范围。您可以放入int simple的最大数字与您的数字所代表的50亿值不符

试试长时间

或者,更好的方法是:使用一个特定的类来表示电话号码,例如,使用一个仅包含该号码的数字的数组,但也保留从用户处获得的完整初始字符串


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

可以的最大值
int
是2147483647。(请参阅)。您的示例电话号码远大于此值。请尝试使用
Long.parseLong()
相反,因为长
的最大值为9223372036854775807。

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


似乎整数运算太多了-可能创建一个电话类,将前缀等作为单独的字段/属性,然后对其进行操作。可能使用字符串会更好,除非您需要进行一些数学运算,不建议进行转换。是的,这就是问题所在。非常感谢!然后继续并接受回答你认为对你有帮助。是的,这就是问题。非常感谢!