2013-11-09 77 views
0

我需要幫助將字符串轉換爲int,我搜索了 並沒有發現任何可以幫助我處理這個特定問題的東西。信用卡驗證

這是我走到這一步。(代碼下)

  • 該卡必須是6位長(如123456) - 得到它
  • 我已經分離出的卡的每個數字數字(數字,數十,數百...)
  • 我使用字符串組合了卡的前五位數字(從左邊開始)。

現在結合第一5位後(例如,1 + 2 + 3 + 4 + 5 = 12345) 我需要在7(12345%7)

到MOD這個號碼,但不斷給我下一個錯誤 -

java.lang.number.formatexception 輸入字符串:fiveDigits:(在java.lang.number.formatexception)

就如何解決它的任何準則將非常感激。 謝謝。

if(creditCard<=99999||creditCard>=1000000) 
     System.out.println("Your credit card is not valid. You cannot buy the ticket"); 
     else 
     { 

       ones = creditCard%10; 
       tens = (creditCard%100)/10; 
       hundreds = (creditCard%1000)/100; 
       thousands = (creditCard%10000)/1000; 
       tensOfThousands = (creditCard%100000)/10000; 
       hunOfThousands = (creditCard%1000000)/100000; 

       String fiveDigits = "" + hunOfThousands+tensOfThousands+thousands+hundreds+tens; 
       int firstFiveDigits = Integer.parseInt("fiveDigits"); 
       int remainder = firstFiveDigits%7; 


       if(remainder==ones) 
       System.out.println("Your credit card is valid. Bon Voyage!"); 
       else 
        System.out.println("Your credit card is not valid. You cannot buy the ticket"); 
+0

您正在解析字符串「fiveDigits」,而不是由變量fiveDigits表示的字符串。刪除引號,你會沒事的。 –

回答

1

卸下引號包圍可變fiveDigits

INT firstFiveDigits =的Integer.parseInt(fiveDigits);

0
int firstDigits = (allDigits - allDigits%10)/10; 

這是不好的,但它的更好......

你可以用「真實」的CC數爲字符串操作(因爲它們是相當長的) - 只是讓你allDigitsAsString作爲輸入,然後

String firstDigitsAsString = allDigitsAsString.substring(0, allDigitsAsString.length() - 1); 
int firstDigits = Integer.parseInt(firstDigitsAsString); 

對於獎勵積分,將一些模仿電話添加到付款處理器存根。