我需要帮助将字符串转换为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");
您正在解析字符串“fiveDigits”,而不是由变量fiveDigits表示的字符串。删除引号,你会没事的。 –