Java I';我不明白我的意思;我做错了。如何在字符串中存储int而不获取erro?
我正在用java为这个类编写一个程序。我已经编写了代码的第一部分,但我不太清楚我做错了什么。当我运行该程序时,它允许我输入卡号,但随后它会给我一个错误。我的教授说,这是因为对于这个程序来说,卡号太长,不能存储为int。我理解这一点,所以它存储在字符串中。但我还是犯了一个错误。请帮忙Java I';我不明白我的意思;我做错了。如何在字符串中存储int而不获取erro?,java,Java,我正在用java为这个类编写一个程序。我已经编写了代码的第一部分,但我不太清楚我做错了什么。当我运行该程序时,它允许我输入卡号,但随后它会给我一个错误。我的教授说,这是因为对于这个程序来说,卡号太长,不能存储为int。我理解这一点,所以它存储在字符串中。但我还是犯了一个错误。请帮忙 package creditCard; import java.util.Scanner; public class Card { public static void main(String[] ar
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);
}
}
}
您的问题是parseInt。您可以使用BigInteger解决此问题,如
BigInteger bi = new BigInteger(potentialCCN);
或者,您可以使用正则表达式验证potentialCCN是否为16位数字,如
^[0-9]{16,16}$如果您输入的卡号大于
整数。值为2147483647
的最大值,那么这行代码(复制到下面)总是会给您抛出一个错误
其原因是int
无法保存该值,但仅限于2^31− 1
或如上所述,2147483647
。因此,如果您有资格输入诸如卡号之类的值,您应该使用没有此类约束的类型。如果您想要最后一个数字,您已经将子字符串向后,它将删除该数字之前的所有内容,然后将其存储回您的卡号变量中
您还解析了整个数字,而不是最后一个数字
试试下面的方法
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);
你永远不会把信用卡号码当作一个数字,即使它都是数字,所以把它当作一个字符串
我和安德烈亚斯的意见相同。
您可以执行substring
操作以获取要验证的卡号部分,然后执行Long.parseLong()
我在你的代码中看到的其他东西:
- 首选
BufferedReader
和InputStream
()而不是Scanner
,因为它速度更快
- 写尽可能少的注释,因为在更改源代码后,如果您忘记更改注释,注释就会过时。如果您专门为stackoverflow用户添加了它们,那么这不是问题;)李>
您会遇到什么错误?您输入了哪个号码?信用卡号码通常是15-16位,但最多可以是19位。int
只能存储9-10位数字,因此parseInt()
将抛出NumberFormatException
,因为该值超出了int
支持的范围。您可以使用long
或biginger
,但为什么呢?你永远不会把信用卡号码当作一个数字,即使它都是数字,所以把它当作一个字符串
,这里我假设(因为变量名)您想要选择“最后一位”,但是parseInt()
正在解析整个字符串。我同意@Andreas,您可以使用字符串并为检查选择所需的数字。@Andreas OP似乎希望提取/解析Luhn算法的最后一个数字,这可能会解决int问题,但仍然没有意义。没有必要将卡号设置为int或biginger。“验证潜在CCN是否为16位数字”并非所有信用卡号都是16位数字。它们可以介于两者之间。我不确定真正的卡可以有前导零,但这仍然是可能的,因此int/bigint不会起作用。关于BufferedReader和Scanner的重拍通常是正确的,但在从System.in读取时没有任何区别,特别是当用户手动将输入写入控制台窗口时。
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);