英文:
I'm not understanding what I'm doing wrong. How do you store an int in a String and not get an erro?
问题
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 validCreditCard 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(0, 15);
// Test then comment out! - check potential credit card number
System.out.println(potentialCCN);
}
}
}
英文:
I'm using java to write a program for class. I have the first portion of code written but I can't quite figure out what I'm doing wrong. When I run the program it lets me enter a card number but then it gives me an error. My professor said that it's because for this program the card number is too long to be stored as an int. I understand that, so it's being stored in a String. But I'm still getting an error. Help please.
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);
}
}
}
答案1
得分: 0
你的问题出在 parseInt 上。你可以使用 BigInteger 来解决这个问题,就像这样:
BigInteger bi = new BigInteger(potentialCCN);
或者你可以使用正则表达式来验证 potentialCCN 是否为一个 16 位数字,就像这样:
^[0-9]{16,16}$
英文:
Your problem is with parseInt. You can overcome this problem using BigInteger, like
BigInteger bi = new BigInteger(potentialCCN);
or you can use regular expressions to validate that potentialCCN is a 16 digit number, like
^[0-9]{16,16}$
答案2
得分: 0
如果您输入的卡号作为卡号的数字大于**Integer.MAX_VALUE
,其值为*2147483647
***,那么下面这行代码(复制如下)将始终抛出NumberFormatException。
int lastDigit = Integer.parseInt(potentialCCN);
原因是**int
**无法容纳如此大的值,而是限制为2^31 − 1
,如上所述,为2147483647
。因此,如果您有资格将这样的值输入为卡号,则应使用一些没有此类约束的类型。
英文:
If the number you entered as a card number is larger than Integer.MAX_VALUE
that has a value of 2147483647
, then this line of code (copied below) is always going to throw you a NumberFormatException.
int lastDigit = Integer.parseInt(potentialCCN);
The reason for it is that int
is not able to hold such a value, but is restricted to 2^31 − 1
or as written above, 2147483647
. Therefore, if you are eligible to enter such values as card numbers you should use some type that does not have such constraints.
答案3
得分: 0
如果你想要最后一位数字,你的子字符串是颠倒的,它会移除直到那个数字,然后将其存回你的卡号变量。
你还在解析整个数字,而不是获取最后一位数字。
尝试以下代码:
String potentialCCN = in.nextLine();
// 使用 Luhn 算法验证信用卡
// 将数字作为整数存储,以备稍后使用
int lastDigit = Integer.parseInt(potentialCCN.substring(potentialCCN.length() - 2));
// 测试后注释掉! - 检查最后一位数字
System.out.println(lastDigit + " 检查最后一位数字");
// 从 potentialCCN 中移除最后一位数字,并使用 String 的 substring 方法存储回 potentialCCN
potentialCCN = potentialCCN.substring(0, potentialCCN.length() - 2);
英文:
If you want the last digit, you've got your substring backwards, it's removing everything up to that digit, then storing it back in your card number variable.
You're also parsing the entire number rather than getting the last digit
Try the following
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);
答案4
得分: 0
> 你永远不会将信用卡号视为数值,即使它完全由数字组成,所以请将其保留为字符串。
我和安德烈斯持相同观点。
您可以执行substring
操作以获取要验证的卡号部分,然后执行Long.parseLong()
我在您的代码中还看到了其他一些事情:
- 建议使用
BufferedReader
和InputStream
(https://stackoverflow.com/a/34955813/7192651),因为它更快 - 遵循干净代码原则,尽量少写注释,因为注释会过时,如果您在更改源代码后忘记修改它们的话。如果您是为了stackoverflow用户而添加这些注释,那就没问题了
英文:
> You're never going to be treating a credit card number as a numerical value, even though it's all digits, so keep it as a String.
I have the same opinion as Andreas.
You can do a substring
operation to get the part of the cardNumber that you want to validate, and then do a Long.parseLong()
Other thingys that I saw in your code:
- prefer
BufferedReader
andInputStream
(https://stackoverflow.com/a/34955813/7192651) overScanner
because it is more faster - write as few comments as possible as clean code principles suggest, because comments become outdated, if you forget to change them, after changing your source code. If you added them specifically for stackoverflow users sake then that is not a problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论