作爲我的虛構銀行的登錄方法的一部分,我設置了一個Luhn algorithm來驗證用戶ID。ArrayList-「for-loop」在.size()調用處退出代碼,出了什麼問題?
好像檢查過,回來有效的,但是當我列出ArrayList
(與for
-loop),看看是否有一個相應的匹配與否,代碼似乎跳出當它擊中kList.size()
看到這個:
public void logIn() {
System.out.print("Please enter your ID (10 numbers):");
String x = s.nextLine();
if (luhnCheck(x)) {
for (i = 0; i < k.kList.size(); i++) { //<-----ISSUE!
if (k.kList.get(i).getPnr().equals(x)) {
tempKund = k.kList.get(i);
}
}
} else {
System.out.println("You are not a customer, please register!");
System.out.print("Enter name:");
String n = s.nextLine();
k.createKund(x, n); //sends values to create customer method
kundMeny1(); // customer menu...
}
}
public boolean luhnCheck(String v) {
int sum = 0;
boolean alternate = false;
for (int i = v.length() - 1; i >= 0; i--) {
int n = Integer.parseInt(v.substring(i, i + 1));
if (alternate) {
n *= 2;
if (n > 9) {
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}
更新:因此很明顯,這個問題似乎並不在循環,但是當.size()試圖獲取所需要的信息。我會貼一些我的代碼:
public class Bank {
Scanner s = new Scanner(System.in);
Kund k = new Kund(); //Used for communicating with the Kund(customer class)
Konto t = new Konto(); //Used for communicating with the Konto(account class)
Kund tempKund; //Temporary customer used to keep track of who's logged in
int i;
public static void main(String[] args) {
Bank b = new Bank();
b.mainMenu();
}
public void mainMenu() {
k.createKund("8908041207", "Adam Sears"); //Creates a customer
t.createKonto("1234567891", "3000"); //Creates a bank account
int user_choice = 3;
do { // Goes on to a Switch Case menu for the user...
昆德(Customer類)
public class Kund {
ArrayList<Kund> kList = new ArrayList<Kund>();
Kund knd;
String pnr; //Customer ID, used in validation
String name; //Customer name
public void kund() {
}
public String getPnr() {
return pnr;
}
public void setPnr(String x) {
this.pnr = x;
}
public String getName() {
return name;
}
public void setName(String z) {
this.name = z;
}
public void createKund(String p, String n) { //Creates the new customer
knd = new Kund();
knd.setPnr(p);
knd.setName(n);
addKund(knd);
}
public ArrayList<Kund> addKund(Kund s) { //Adds said customer to ArrayList
kList.add(s);
return kList;
}
什麼是k參考? –
用調試器跟蹤代碼是找到問題源的方法 –