2017-03-05 6 views
-1

Integer.parseInt()を使用して、phonenumberを表す文字列を、私が使用できるintに変換しようとしています。intに変換する際にJava文字列の生成エラーが発生しました

これは私が取得していますエラーです:

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を初めて使っているので、それがなぜ、あるいは私が間違っていたのか分かりません。

答えて

0

最大値intは2,147,483,647です。 (Integer.MAX_VALUEを参照してください)あなたの電話番号はそれよりはるかに大きいです。代わりにLong.parseLong()を使用してください。longの最大値は9,223,372,036,854,775,807です。

+0

はい、これが問題でした。どうもありがとうございました! –

0

範囲外です。単純なintに収まる最大の数は、あなたの数値が表す50億の値には合致しません。

代わりに長くしてください。

また、特定のクラスを使用して電話番号を表すこともできます。たとえば、その番号の数字だけを含む配列を使用するなど、ユーザーから取得した完全な初期文字列を保持するなどです。

実際に電話番号に数字が含まれているということは、電話番号を実際の番号として保存することを意味しません。

+0

はいこれは問題でした。どうもありがとうございました! –

+0

それから、あなたに役立つと思われる答えを受け入れてください。 – GhostCat

0

入力がIntegerが格納(格納)できる最大値を超えています。これを乗り切るには、データ型をlongに変更します。

あまりにも多くの整数演算があるようです - 電話のクラスを作成し、接頭辞などを別のフィールド/属性として設定してから操作してください。たぶん、文字列の使用は、数学的演算を行う必要がない限り、より良いでしょう。変換はお勧めできません。

関連する問題