java - 信用卡验证

标签 java string int

我需要帮助从字符串转换为整数,我已经搜索过 并且没有找到任何可以帮助我解决这个具体问题的内容。

这就是我到目前为止所得到的..(代码向下)

  • 该卡的长度必须为 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/

相关文章:

java - 下载文件时堆空间异常

java - 之后使用 switch 分割字符串

python - python在哪里存储str的原始值

c - 如何在 C 中将 char 转换为整数?

java - 如何在 JAXB XML 解码期间实现子对象的父对象属性(属性和值)

java - 请求安全页面时,RestTemplate 不会因 403 或 302 错误而失败

java - 在 Spring Boot 中急切加载单例对象,无需注释

c++ - 使用strtok按条件拆分字符串?

c++ - 将字符串转换为 int 使用 sstream

algorithm - 在精度损失后从 int 中恢复 long