如何在java中制作待办事项列表?

How to make a To-Do List in java?

提问人:bean 提问时间:6/16/2020 更新时间:11/7/2023 访问量:11997

问:

我需要在 java 中创建一个有效的待办事项列表,允许用户:

  1. 添加项目

  2. 删除项目

  3. 显示列表

  4. 删除所有任务

  5. 退出程序

我目前在将项目添加到我的列表中时遇到问题。每次我输入要添加的项目时,我都会收到以下输出:

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Test.menu(Test.java:50)
at Test.main(Test.java:11)

这是我到目前为止尝试过的,任何帮助将不胜感激:

import java.util.Scanner;
import java.util.ArrayList;

public class Test {

public static void main(String[] args) {

    int menuItem = -1;

    while(menuItem !=0) {
        menuItem = menu();

        switch(menuItem) {

        case 1: 
            showList();
            break;

        case 2: 
            addItem();
            break;

        case 3: 
            removeItem();
            break;

        case 0: 
            break;

        default: 
            System.out.println("Enter a valid option");
        }

    }
}

public static int menu() {

int choice; 

Scanner keyboard = new Scanner(System.in);
System.out.println("Main Menu");
System.out.println();
System.out.println("0. Exit the program");
System.out.println("1. Display to-do list");
System.out.println("2. Add item to list");
System.out.println("3. Remove item from list");
System.out.println();
System.out.print("Enter choice: ");
choice = keyboard.nextInt();

return choice;
}

public static void showList() {

System.out.println("To-Do List");

Scanner input = new Scanner(System.in);
String line;
int number = 1;

while (input.hasNextLine()){
    line = input.nextLine();
    System.out.println(number + " ");
    System.out.println(line);
    ++number;
}

System.out.println();


}

public static void addItem() {

System.out.println("Add Item");

Scanner input = new Scanner(System.in);
System.out.println("Enter an item: ");
String item = input.nextLine();
System.out.println(item);


}

public static void removeItem() {

int choice;
showList();

Scanner input = new Scanner(System.in);
System.out.println("What do you want to remove?");
choice = input.nextInt();

ArrayList<String> items = new ArrayList<String>();
int number = 1;

Scanner input2 = new Scanner(System.in);
String item; 

while (input2.hasNextLine()) {
    item = input2.nextLine();

    if (number != choice) 
        items.add(item);

    ++number;   
}

for(int i = 0; i < items.size(); i++)
    System.out.println(items.get(i));

    }
}
数组列表 java.util.scanner

评论

0赞 MarsAtomic 6/16/2020
这回答了你的问题吗?扫描仪在使用 next() 或 nextFoo() 后跳过了 nextLine()?
1赞 MarsAtomic 6/16/2020
发布问题时,请确保有条不紊地列出重现问题所需的每个步骤,包括错误消息的确切文本,并指出错误消息抱怨的代码行。你不能假设人们会运行你的代码来查看问题所在——事实上,大多数提供答案的人都在不运行代码的情况下阅读了代码。
0赞 Alastair McCormack 11/7/2023
你的实际问题是什么?

答:

0赞 Jimmy 6/17/2020 #1

您缺少从列表中添加删除的实际操作。@DrMe通过这些程序,您可以找到您做错的地方。希望这能帮到你

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestToDoList {

    private static List<String> currentList = new ArrayList<String>();

    public static void main(String[] args) {

        int menuItem = -1;
        while (menuItem != 0) {
            menuItem = menu();
            switch (menuItem) {
            case 1:
                showList();
                break;
            case 2:
                addItem();
                break;
            case 3:
                removeItem();
                break;
            case 0:
                break;
            default:
                System.out.println("Enter a valid option");
            }
        }
    }

    public static int menu() {
        System.out.println();
        System.out.println("----------------------");
        System.out.println("Main Menu");
        System.out.println("----------------------");
        System.out.println("0. Exit the program");
        System.out.println("1. Display to-do list");
        System.out.println("2. Add item to list");
        System.out.println("3. Remove item from list");
        System.out.println();
        System.out.print("Enter choice: ");
        int choice = scanner.nextInt();
        return choice;
    }

    public static void showList() {
        System.out.println();
        System.out.println("----------------------");       
        System.out.println("To-Do List");
        System.out.println("----------------------");
        int number = 0;
        for (String item : currentList) {
            System.out.println(++number + " " + item);
        }
        System.out.println("----------------------");


    }

    public static void addItem() {
        System.out.println("Add Item");
        System.out.println("----------------------");
        System.out.print("Enter an item: ");
        Scanner scanner = new Scanner(System.in);
        String item = scanner.nextLine();
        currentList.add(item);
        showList();
    }

    public static void removeItem() {
        System.out.println("Remove Item");
        System.out.println("----------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("What do you want to remove?");
        int index = scanner.nextInt();
        if((index-1)<0 || index>currentList.size()) {
            System.out.println("Wrong index number! Please enter in range of 1 to "+currentList.size());            
        }else {
            currentList.remove(index-1);
        }
        System.out.println("----------------------");
        showList();


    }
}

使用 generic 和 utils 方法进一步改进。