简体   繁体   English

根据ArrayList验证用户输入

[英]Validate user input against ArrayList

I am having issues with the following part of my code. 我的代码的以下部分出现问题。 when "nn" is entered i get invalid code. 当输入“ nn”时,我得到无效代码。 when valid code is entered i get invalid code however this only happens once. 当输入有效代码时,我得到无效代码,但是这只会发生一次。 program doesn't seem to work as intended. 程序似乎无法正常工作。 Please assist. 请协助。

    System.out.println("ENTER CODE (nn to Stop) : ");
    ArrayList<Product> list = new ArrayList<Product>();
    .
    .
    .
    .


    ArrayList<Code> codeList = new ArrayList<Code>();


    for (Product product : list) {
        System.out.print("CODE : ");
        String pcode = scan.next();
        if (pcode.equalsIgnoreCase("nn")) {
            break;
        }

        if (!(code.equalsIgnoreCase(product.getCode()))) {
            System.out.println("Invalid code, please enter valid code.");
            System.out.print("CODE : ");
            pcode = scan.next();

        }

        System.out.print("QUANTITY : ");
        int quan = scan.nextInt();
        while (quan > 20) {
            System.out.println("Purchase of more than 20 items are not allowed, please enter lower amount.");
            System.out.print("QUANTITY : ");
            quan = scan.nextInt();
        }
        codeList.add(new Code(pcode, quan));
    }

You want continue instead of break . 您要continue而不是break

Also, you should only call code = scan.next() once inside the loop; 同样,您应该只在循环内调用一次code = scan.next() otherwise you'll skip over some items. 否则,您将跳过一些项目。

String code = scan.next();
boolean match = false;
for (Product product : list) {
    if (code.equalsIgnoreCase(product.getCode())) {
        match = true;
        break;
    }
}
// now only if match is false do you have an invalid product code.

Update: 更新:

I still can't get this to work. 我仍然无法使它正常工作。 What I am trying to do is test user input to make sure that product code exists, if not prompt that the product code entered is invalid and asks for correct code. 我想做的是测试用户输入,以确保产品代码存在,如果没有提示输入的产品代码无效并要求输入正确的代码。 I also need to have the condition to stop order when "nn" is entered. 输入“ nn”时,我还需要具备停止订单的条件。 I have tried while loops, do-while loops etc. i can't seem to get it right. 我尝试了while循环,do-while循环等。我似乎无法正确地做到这一点。 Please assist. 请协助。 My problem is with writing code for multiple conditions. 我的问题是为多个条件编写代码。 When one is working correctly the other isn't. 当一个正常工作时,另一个则无法正常工作。

while (true) {
    final String code = scan.next();
    if (isExitCode(code)) {
        break;
    }
    if (!isValidCode(code)) {
        System.out.println("Invalid code, please enter valid code.");
        continue;
    }
    int quantity = -1;
    while (true) {
        quantity = scan.nextInt();
        if (!isValidQuantity(quantity)) {
            System.out.println("bad quantity");
            continue;
        }
        break;
    }
    // if you've got here, you have a valid code and a valid 
    // quantity; deal with it as you see fit.
}

Now you just need to write the methods isExitCode(), isValidCode(), and isValidQuantity(). 现在,您只需要编写方法isExitCode(),isValidCode()和isValidQuantity()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM