我需要帮助从字符串转换为整数,我已经搜索过 并且没有找到任何可以帮助我解决这个具体问题的内容。
这就是我到目前为止所得到的..(代码向下)
- 该卡的长度必须为 6 位数字(例如 123456) - 明白
- 我已隔离卡号的每个数字(个位、十位、百位...)
- 我使用字符串组合了卡的前五位数字(从左侧开始)。
现在组合前 5 位数字后(例如 1+2+3+4+5=12345) 我需要将这个数字修改为 7 (12345%7)
但它一直给我下一个错误 -
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 周围的引号
int firstFiveDigits = Integer.parseInt( FiveDigits);
关于java - 信用卡验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19880084/