【问题标题】:I'm not understanding what I'm doing wrong. How do you store an int in a String and not get an erro?我不明白我做错了什么。如何将 int 存储在 String 中而不出错?
【发布时间】:2020-10-14 15:15:18
【问题描述】:

我正在使用 java 为类编写程序。我已经编写了第一部分代码,但我无法弄清楚我做错了什么。当我运行该程序时,它让我输入一个卡号,但它给了我一个错误。我的教授说这是因为对于这个程序,卡号太长而无法存储为 int。我明白这一点,所以它被存储在一个字符串中。但我仍然遇到错误。请帮忙。

package creditCard;

import java.util.Scanner;

public class Card {

    public static void main(String[] args) {
        //Set up a scanner
        Scanner in = new Scanner(System.in);
        
        //Set up a boolean named creditCard and set it to false
        boolean validCreditCard = false;
        
        //Loop while not a valid credit card
        while (!validCreditCard) {
            
            //Prompt the user for a potential credit card number
            System.out.print("Please enter a card number: ");
            
            //Get the credit card number as a String - store in potentialCCN 
            String potentialCCN = in.next();
            
            //Use Luhn to validate a credit card
            //Store the digit as an int for later use in lastDigit
            int lastDigit = Integer.parseInt(potentialCCN);
            
            //Test then comment out! -check the last digit
            System.out.println(lastDigit+"Check the last digit");
            
            //Remove the last digit from potentialCCN and store in potentialCCN using String's substring
            potentialCCN = potentialCCN.substring(15);
            
            //Test then comment out! - check potential credit card number
            System.out.println(potentialCCN);
            
        }

    }

}

【问题讨论】:

  • 你得到什么错误?你输入了哪个号码?
  • 信用卡号一般为15-16位,但最长可达19位。一个int只能存储9-10位数字,所以parseInt()会抛出NumberFormatException,因为该值超出了int支持的范围。您也许可以使用longBigInteger,但为什么呢?您永远不会将信用卡号视为数值,即使它全是数字,所以请将其保留为 String
  • 我认为问题可能出在int lastDigit = Integer.parseInt(potentialCCN);,这里我假设(由于变量名)您想选择“最后一位”,但parseInt() 正在解析整个字符串。我同意@Andreas,您可以使用 String 并为您的支票选择所需的数字。
  • @Andreas 看来 OP 想要提取/解析 Luhn 算法的最后一位数字

标签: java


【解决方案1】:

如果您想要最后一个数字,则将子字符串倒过来,它会删除该数字之前的所有内容,然后将其存储回您的卡号变量中。

您也在解析整个数字,而不是获取最后一位数字

试试下面的

        String potentialCCN = in.nextLine();
        
        //Use Luhn to validate a credit card
        //Store the digit as an int for later use in lastDigit
        int lastDigit = Integer.parseInt(potentialCCN.substring(potentialCCN.length()-2);
        
        //Test then comment out! -check the last digit
        System.out.println(lastDigit+"Check the last digit");
        
        //Remove the last digit from potentialCCN and store in potentialCCN using String's substring
        potentialCCN = potentialCCN.substring(0, potentialCCN.length()-2);
        

【讨论】:

    【解决方案2】:

    您的问题在于 parseInt。你可以使用 BigInteger 来解决这个问题,比如

    BigInteger bi = new BigInteger(potentialCCN);
    

    或者你可以使用正则表达式来验证potentialCCN是一个16位数字,比如

    ^[0-9]{16,16}$

    【讨论】:

    • 这可能会解决 int 问题,但仍然没有意义。将卡号设置为 int 或 BigInteger 是没有意义的。
    • “验证potentialCCN 是16 位数字” 并非所有信用卡号码都是16 位数字。它们可以在12 and 19 digits 之间。
    • 我不确定真正的卡片是否可以有前导零,但仍有可能,因此 int/bigint 不起作用
    【解决方案3】:

    如果您作为卡号输入的号码大于值为 2147483647Integer.MAX_VALUE,则此代码行(复制在下面)总是会给你一个NumberFormatException

             int lastDigit = Integer.parseInt(potentialCCN);
    

    其原因是 int 无法保持这样的值,而是限制为 2^31 − 1 或如上所述,2147483647。因此,如果您有资格输入诸如卡号之类的值,您应该使用一些没有此类限制的类型。

    【讨论】:

      【解决方案4】:

      您永远不会将信用卡号视为数值,即使它全是数字,所以请将其保留为字符串。

      我和安德烈亚斯有同样的看法。 您可以执行substring 操作以获取要验证的cardNumber 部分,然后执行Long.parseLong()

      我在您的代码中看到的其他内容:

      • 更喜欢BufferedReaderInputStream (https://stackoverflow.com/a/34955813/7192651) 而不是Scanner,因为它更快
      • 按照简洁代码原则的建议,尽可能少地编写 cmets,因为如果在更改源代码后忘记更改 cmets,cmets 就会过时。如果您专门为 stackoverflow 用户添加它们,那么这不是问题 ;)

      【讨论】:

      • 您对 BufferedReader 和 Scanner 的重新制作通常是正确的,但在从 System.in 读取时没有区别,尤其是当用户手动将输入写入控制台窗口时。
      猜你喜欢
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2011-07-04
      • 1970-01-01
      • 2018-06-19
      相关资源
      最近更新 更多