【问题标题】:ArrayList-"for-loop" exits code at .size() call, what's wrong?ArrayList-“for-loop”在 .size() 调用时退出代码,怎么了?
【发布时间】:2015-10-16 21:26:26
【问题描述】:

作为我虚构银行的登录方法的一部分,我设置了Luhn algorithm 来验证用户 ID。

它似乎检查通过并返回有效,但是当我列出ArrayList(带有for-loop)以查看是否存在相应的匹配时,代码似乎在遇到@时中断987654324@

看到这个:

    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... 

昆德(客户类)

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参考?
  • 用调试器跟踪代码是找到问题根源的方法

标签: java for-loop arraylist


【解决方案1】:

这部分代码:

    if (luhnCheck(x)) {
        if (true) {
          // Code
        } else if (false) {
          // Code
        }
    }

应该换成这个:

    if (luhnCheck(x)) {
        // Code
    } else {
        // Code
    }

剩下的问题。我应该知道你在哪里实例化你的 k 对象。

【讨论】:

  • 感谢您抽空 Yassin。它没有解决问题,我的代码仍然跳过并返回主菜单。我会用更多信息更新这个问题。
【解决方案2】:

我们需要更多关于 k 是如何实例化的信息,尝试组织你的 sn-p 代码,并尝试在

 for (i = 0; i < k.kList.size(); i++) { //<-----ISSUE!

看看 kist 包含什么?

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 2017-09-06
    • 2012-04-10
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多