java - 嵌套 For-each 循环打印 Arraylist 中的每个项目

标签 java foreach

如何修改此代码,以便它仅在首先迭代整个列表后才打印出无效消息。我当前的问题是,每次不匹配时它都会打印出这些消息。这不是我想要做的。

public void assignSchedule(Depot depotName) throws Exception {
        String client, driver;
        Boolean exit = false;

        do {
            System.out.printf("%-10s %-10s %10s %n", "Client", "Start Date", "End Date");
            depotName.listWorkSchedulue();
            System.out.print("Enter the client name for the schedule you wish to assign: ");
            client = DepotSystem.input.nextLine();

            for (WorkSchedule ws : depotName.getWorkSchedules())
            {
                if (client.equals(ws.getClient()))
                {
                    System.out.printf("%n%20s%n", "Drivers");
                    depotName.listDrivers();
                    System.out.print("Which Driver do you want to assign this to: ");
                    driver = DepotSystem.input.nextLine();

                    for (Driver drivers : depotName.getDrivers())
                    {
                        if (driver.equals(drivers.userName) && !drivers.assigned)
                        {
                            System.out.printf("%nThe work schedule for client %s is now assigned to %s%n ", client,
                                driver);
                            depotName.getDriver(driver).setSchedule(depotName.getWorkSchedule(client));
                            drivers.setAssigned(true);
                            exit = true;
                        } else if ((!driver.equals(drivers.userName) && drivers.assigned)) {
                            System.out.printf("%nEither not a valid driver or Driver is already assigned a Job.%n");
                        }
                    }
                } else if (!client.equals(ws.getClient())) {
                    System.out.printf("%nPlease enter a valid client.%n");
                }
            }
        } while (!exit);
    }

如果我输入错误的名称,当前输出如下

Client     Start Date   End Date 
Gary       2020-04-25   2020-04-27
Bob        2020-05-05   2020-05-06
Enter the client name for the schedule you wish to assign: f

Please enter a valid client.

Please enter a valid client.

这就是我希望在输入错误名称时它的工作方式。

Client     Start Date   End Date 
Gary       2020-04-25   2020-04-27
Bob        2020-05-05   2020-05-06
Enter the client name for the schedule you wish to assign: f

Please enter a valid client.

最佳答案

在 for 循环的顶部,您可以执行以下操作来确定是否有任何工作计划包含输入的客户。您随后可以将 depotName.getWorkSchedules() 替换为 clientSchedules 并删除条件 if (client.equals(ws.getClient())) 因为我们知道它们都是计划与同一个客户。

请注意,该问题没有包含完整的SSCCE,缺少编译所需的许多基本类,因此可能会出现意外的编译错误。

            List<WorkSchedule> clientSchedules = depotName.getWorkSchedules().stream()
                            .filter(schedule -> client.equals(schedule.getClient()))
                            .collect(Collectors.toList());

            if (clientSchedules.isEmpty()) {
                System.out.printf("%nPlease enter a valid client.%n");
                continue;
            }

关于java - 嵌套 For-each 循环打印 Arraylist 中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61363979/

相关文章:

java - 从实例化对象调用主类中的方法

java - 在 Java 中创建插件系统

java - 如何在 Java 中实例化具有相互循环引用的类,而不必使用 `NULL`?

java - 在本地开发服务器中运行 AppEngine 应用程序

java - 在 Java 中,如何等待所有任务,但在出现第一个错误时停止?

c# - foreach文件夹中的文件,重命名并保存

php - foreach 循环不适用于单个结果

PHP 警告 - 非法字符串偏移

php - 将两个数组插入到一个mysql表中

php - 每个 foreach javascript/php 之间的延迟