英文:
During doing h,w with Java, an error about String range has raised
问题
public class CreditCard {
private String cardNumber; // 信用卡号码以字符串形式存储。
private boolean valid; // 表示是否有效
int errorCode; // 实例变量
// 初始化实例变量
public CreditCard(String creditCardNumber) {
cardNumber = creditCardNumber;
valid = true; // 初始化实例为真,假定输入的数字可能为假
errorCode = 0;
}
public void check1() {
// 第一个数字必须是4。
if (cardNumber.charAt(0) != '4') {
valid = false;
errorCode = 1; // 错误代码1
return;
}
}
public void check2() {
// 第四位数字必须比第五位数字大1
int fourth = Integer.parseInt(cardNumber.substring(4, 5));
int fifth = Integer.parseInt(cardNumber.substring(5, 6));
if (fourth != fifth + 1) {
valid = false;
errorCode = 2;
return;
}
}
public void check3() {
// 第一个、第五个和第九个数字的乘积必须为24
int first = Integer.parseInt(cardNumber.substring(0, 1));
int fifth = Integer.parseInt(cardNumber.substring(4, 5));
int ninth = Integer.parseInt(cardNumber.substring(8, 9));
if (first * fifth * ninth != 24) {
valid = false;
errorCode = 3;
return;
}
}
public void check4() {
// 所有数字的和必须能被4整除
int sum = 0; // 初始化sum变量
for (int i = 0; i < cardNumber.length(); i++)
sum = sum + Character.getNumericValue(cardNumber.charAt(i));
if (sum % 4 != 0) {
valid = false;
errorCode = 4;
return;
}
}
public void check5() {
// 前四个数字的和必须比后四个数字的和少1
int sumFirst = 0;
int sumLast = 0;
for (int i = 0; i < 4; i++)
sumFirst = sumFirst + Character.getNumericValue(cardNumber.charAt(i));
for (int i = 0, j = cardNumber.length() - 1; i < 4; i++, j--)
sumLast = sumLast + Character.getNumericValue(cardNumber.charAt(j));
if (sumFirst != sumLast - 1) {
valid = false;
errorCode = 5;
return;
}
}
public void check6() {
// 如果将前两位数字视为两位数,将第七和第八位数字视为两位数,它们的和必须为100。
int first = Integer.parseInt(cardNumber.substring(0, 2));
int seight = Integer.parseInt(cardNumber.substring(6, 8));
if (first + seight != 100) {
valid = false;
errorCode = 6;
return;
}
}
public void check() {
check1();
check2();
check3();
check4();
check5();
check6();
}
public boolean isValid() {
return valid;
}
public int getErrorCode() {
return errorCode;
}
}
英文:
I made the codes for a credit card tester. while I compile these codes, it has been compiled successfully. however, when I enter 12 digits of credit card numbers, it showed a message: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11. I would really appreciate it if you guys tell me what has been wrong on the codes. Thank you
public class CreditCard{
private String cardNumber; //The credit card number is stored as String.
private boolean valid; //To indicate valid or not
int errorCode; //instance variable
// initializing instance variables
public CreditCard(String creditCardNumber){
cardNumber = creditCardNumber;
valid = true; //initial instance = true, assumes that inputted numbers can be false
errorCode = 0;
}
public void check1(){
//The first digit must be a 4.
if(cardNumber.charAt(0) != 4){
valid = false;
errorCode = 1; //case 1 error
return;
}
}
public void check2(){
//The fourth digit must be one greater than the fifth digit int fourth = Integer.parseInt(4,5)
int fourth = Integer.parseInt(cardNumber.substring(4,5));
int fifth = Integer.parseInt(cardNumber.substring(5,6));
if(fourth != fifth + 1){
valid = false;
errorCode = 2;
return;
}
}
public void check3(){
//The product of the first, fifth, and ninth digits must be 24
int first = Integer.parseInt(cardNumber.substring(1,2));
int fifth = Integer.parseInt(cardNumber.substring(5,6));
int ninth = Integer.parseInt(cardNumber.substring(9,10));
if(first * fifth * ninth != 24){
valid = false;
errorCode = 3;
return;
}
}
public void check4(){
//The sum of all digits must be evenly divisible by 4
int sum = 0; //initiate variable sum
for(int i=0; i<cardNumber.length(); i++ )
sum = sum + Character.getNumericValue(cardNumber.charAt(i));
if(sum % 4 != 0){
valid = false;
errorCode = 4;
return;
}
}
public void check5(){
// The sum of the first four digits must be one less than the sum of the last four digits
int sumFirst = 0;
int sumLast = 0;
for(int i = 0; i<4; i++)
sumFirst = sumFirst + Character.getNumericValue(cardNumber.charAt(i));
for(int i = 0, j = cardNumber.length(); i<4; i++, j-- )
sumLast = sumLast + Character.getNumericValue(cardNumber.charAt(j));
if(sumFirst != sumLast - 1){
valid = false;
errorCode = 5;
return;
}
}
public void check6(){
//If you treat the first two digits as a two-digit number, and the seventh and eight digits as a two digit number, their sum must be 100.
int first = Integer.parseInt(cardNumber.substring(0,2));
int seight = Integer.parseInt(cardNumber.substring(8,10));
if(first + seight != 100){
valid = false;
errorCode = 6;
return;
}
}
public void check(){
check1();
check2();
check3();
check4();
check5();
check6();
}
public boolean isValid(){
return valid;
}
public int getErrorCode(){
return errorCode;
}
}
答案1
得分: 1
当您获取字符串的长度时,它的字符数组从零开始,到长度减1结束;
在您的第五次检查中,您访问了索引为j的字符,基本上超出了字符串数组的范围。
英文:
when u get length of string its array of chars start from zero and end in length-1;
in your fifth check you accessed the char at index j that basically is outside of range of string array
答案2
得分: 0
除了索引越界错误之外,我认为在check1()方法中您漏掉了一些内容。
如果卡号被存储为字符串,那么if(cardNumber.charAt(0) != 4){
这一行应该是if(cardNumber.charAt(0) != '4'){
,因为它在检查字符而不是整数。
英文:
Hi besides the Index Out of Bound Error, I think you missed something in the check1() method.
If the cardNumber is stored as string, the line if(cardNumber.charAt(0) != 4){
should be if(cardNumber.charAt(0) != '4'){
as it's checking the character instead of int.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论