2017-03-05 106 views
-1

我想使用Integer.parseInt()將表示電話號碼的字符串轉換爲我可以使用的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以解決此問題。

似乎太多整數操作 - 也許創建一個電話類,有前綴等,作爲單獨的字段/屬性,然後對他們採取行動。也許字符串的使用會更好,除非你需要做一些數學運算,轉換是不可取的

相關問題