【问题标题】:Finding and editing ArrayList in Java在 Java 中查找和编辑 ArrayList
【发布时间】:2021-08-30 09:17:45
【问题描述】:

我已经编写了一个代码来将用户提供的每个约会日历存储到一个 ArrayList。每个 ArrayList 都包含标题、描述、日期和时间。

现在我想为用户提供一个选项来编辑他们选择的约会日历的数据。

我使用约会日历的标题进行比较。代码如下:

// Asks user to enter the Appointment title that they wish to edit
System.out.println("Please enter the appointment that you wish to edit: ");
String choicetitle = scan.next();

for (Appointment value : aplist)
{
    // if the title of the Appointment matches with one of the data, then..
    if (choicetitle.equalsIgnoreCase(value.title))
    {

        // ..the output occurs on the console and asks the user to enter the new data
        input = new Scanner(System.in);

        System.out.println("Please give the new data: \n");
        System.out.println("Please give the date of the appointment! \n" +
                "Year (format YYYY): ");
        year = input.nextInt();
        System.out.println("Month (format MM): ");
        month = input.nextInt();
        System.out.println("Day (format DD): ");
        day = input.nextInt();
        value.date = LocalDate.of(year, month, day);

        System.out.println("Please give the time of the appointment! \n" +
                "Hour (format hh): ");
        hour = input.nextInt();
        System.out.println("Minute (format mm): ");
        min = input.nextInt();
        value.time = LocalTime.of(hour, min);

        System.out.println("Please give the title of the appointment: ");
        value.title = input.next();

        System.out.println("Please give the description of the appointment: ");
        value.desc = input.next();
    }
    // if the user enters a title that doesn't exist
    else
    {
        System.out.println("There's no appointment with this title!");
    }
}

但是如果 ArrayList 中已经有 3 个元素(3 个约会日历)并且假设我要编辑第三个元素,它会给出以下输出:

  There's no appointment with this title!
  There's no appointment with this title!
  Please give the new data:
  Please give the date of the appointment!
  Year (format YYYY):

有什么办法可以输出:

  1. 仅当用户给出的标题与 ArrayList 中的标题之一匹配时,才给出扫描器方法,
  2. 当用户输入的标题与 ArrayList 中的任何标题都不匹配时,只给出“此标题没有约会”?

提前致谢!

【问题讨论】:

    标签: java for-loop if-statement arraylist


    【解决方案1】:

    看来你对debugging your code的流程不熟悉。所有计算机程序员都需要知道如何做到这一点。如果您使用的是IDE,那么它可能有一个调试器。你应该学习如何使用它。

    您正在for 循环中进行所有处理。您应该首先通过for 循环找到请求的约会。在for 循环终止后,只有当您找到请求的约会时,您才应该处理它。下面是一些帮助您入门的代码。

    System.out.println("Please enter the appointment that you wish to edit: ");
    String choicetitle = scan.next();
    Appointment found = null;
    for (Appointment value : aplist)
    {
        // if the title of the Appointment matches with one of the data, then..
        if (choicetitle.equalsIgnoreCase(value.title))
        {
            found = value;
            break;
        }
    }
    if (value == null) {
        System.out.println("There's no appointment with this title!");
    }
    else {
        System.out.println("Please give the new data: \n");
        System.out.println("Please give the date of the appointment! \n" +
                    "Year (format YYYY): ");
        year = scan.nextInt(); // no need to create a separate `Scanner`
        // rest of your processing code
    }
    

    或者,您可以使用stream API,尽管我认为您还没有了解这一点。下面的代码显示了如何使用 stream API 搜索特定约会。

    aplist.stream()
          .filter(value -> choicetitle.equalsIgnoreCase(value.title))
          .findFirst();
    

    【讨论】:

    • 非常感谢您的回答,它确实有效并且真的帮助了我:) 我是编程新手,所以在您这么说之前我对调试一无所知。我一定会学的。
    猜你喜欢
    • 1970-01-01
    • 2016-03-19
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2015-02-12
    • 2022-08-23
    相关资源
    最近更新 更多