陷入一个持续的循环

Stuck in a constant loop

提问人:Mitchell 提问时间:6/20/2023 更新时间:6/21/2023 访问量:40

问:

我的 StoreFront 类在选择选项 3 返回产品时调用此方法。这种方法应该问我输入产品,把我的控制台再次提示我说。不确定从这里去哪里?为什么我被困在一个持续的循环中?这也是我的方法"Enter the name of the product you want to return: ""Enter the name of the product you want to return: "

    public SalableProduct returnProduct(String name) {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter the name of the product you want to return: ");
        String returnProd = scnr.nextLine();

        SalableProduct productReturned = returnProduct(returnProd);
        if (productReturned == null) {
            System.out.println("Item not in cart");
        } else {
            removeItem(productReturned);
            addItem(productReturned);
            System.out.println(productReturned.getName() + " successfully returned");
        }scnr.close();
        return null;
    }
Java 方法 NullPointerException

评论

1赞 Sweeper 6/20/2023
请举一个最小的可重现示例
0赞 tgdavies 6/20/2023
您显示的代码中没有循环。
2赞 Saurav Dudeja 6/20/2023
你正在递归地调用这个方法 你需要为这一行添加一个中断条件 SalableProduct productReturned = returnProduct(returnProd);
0赞 Mitchell 6/21/2023
感谢您提出中断条件的建议。我还添加了一个 for 循环,用于遍历 salableProduct 列表中的产品。我将把我重构的代码添加到其中。

答:

0赞 Mitchell 6/21/2023 #1

我重构了我的代码并添加了一个 for 循环,这样它就会检查我购物车中的产品,然后在迭代后中断。

    public void returnProduct(String name) {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter the name of the product you want to return: ");
        String returnProd = scnr.nextLine();

        SalableProduct productReturned = null;

        for (SalableProduct product : salableProduct) {
            if (product.getName().equalsIgnoreCase(returnProd)) {
                productReturned = product;
                break;
            }
        }

        if (productReturned == null) {
            System.out.println("Item not in cart");
        } else {
            removeItem(productReturned);
            InventoryManager inventoryManager = new InventoryManager();
            inventoryManager.addProduct(productReturned);
            System.out.println(productReturned.getName() + " successfully returned");
        }

        scnr.close();
    }

另一个错误出现在程序将退出的地方,因为 scnr.close() 与 System.in 相关联,所以我使用自定义 FilterInputStream 并包装 System.in。

更新代码:

    public void returnProduct(String name) {
        FilterInputStream filterInputStream = new FilterInputStream(System.in) {
            @Override
            public void close() throws IOException {
            }
        };
        // FilterInputStream so when scanner is closed it does not quit the program and
        // handles errors.
        Scanner scnr = new Scanner(filterInputStream);
        System.out.println("Enter the name of the product you want to return: ");
        String returnProd = scnr.nextLine();

        SalableProduct productReturned = null;
        for (SalableProduct product : salableProduct) {
            if (product.getName().equalsIgnoreCase(returnProd)) {
                productReturned = product;
                break;
            }
        }

        if (productReturned == null) {
            System.out.println("Item not in cart");
        } else {
            removeItem(productReturned);
            InventoryManager inventoryManager = new InventoryManager();
            inventoryManager.addProduct(productReturned);
            System.out.println(productReturned.getName() + " successfully returned");
        }

        scnr.close();
    }

评论

0赞 David Conrad 6/21/2023
您不应关闭扫描仪,因为它正在您未打开的 System.in 上运行。
0赞 Mitchell 6/21/2023
是的,在我解决了循环问题之后,我实际上只是注意到了这一点。但是我刚刚完成了添加 FilterInputStream 并包装 System.in 以便它覆盖以防止在 scnr 关闭时关闭 System.in。我将添加代码以供参考。