gpt4 book ai didi

java - 如何将多个单词存储为 ArrayList 值

转载 作者:行者123 更新时间:2023-11-30 03:35:21 26 4
gpt4 key购买 nike

 public void addItem() {
System.out.println("Please type the item to add to the To-Do List");
System.out.println();
String newItem = inputread.next();
toDoList.add(newItem);
System.out.println("Your item has been added! Type any key and press Enter to continue");
String discardMe = inputread.next();
menu();
}

这是我的代码,我正在学习 Java 并尝试制作一个小“待办事项列表”类型的程序。按照目前的情况,我一次只能添加一个词。例如,如果我输入“Pick Up Milk”,则 arrayList 仅存储“Pick”。

我尝试使用上面的 inputread.nextLine() ,但随后我得到一个“InputMismatchException”。有什么建议吗?我确信这很简单。

根据要求进行编辑以包括整个类(class):

public class ToDo {
Scanner inputread = new Scanner(System.in);
ArrayList<String> toDoList = new ArrayList<String>();


public void menu() {
clearConsole();
System.out.println("Welcome to the To-Do program.");
System.out.println();
System.out.println();
System.out.println("Please select an option from the following menu, using the number.:");
System.out.println("1- View To-Do List");
System.out.println("2- Add Item To List");
System.out.println("3- Remove Item From List");
int userinput = inputread.nextInt();

switch (userinput) {
case 1:
clearConsole();
displayList();
System.out.println();
System.out.println("This is your list. Type any key and press Enter to continue");
String discardMe = inputread.next();
menu();
break;
case 2:
clearConsole();
addItem();
break;
case 3:
clearConsole();
deleteItem();
break;

}
}

public void clearConsole() {
for (int i = 0; i < 25; i++) {
System.out.println();
}
}


public void addItem() {
System.out.println("Please type the item to add to the To-Do List");
System.out.println();
String newItem = inputread.nextLine();
toDoList.add(newItem);
System.out.println("Your item has been added! Type any key and press Enter to continue");
String discardMe = inputread.next();
menu();
}

public void displayList() {
if (toDoList.isEmpty()) {
System.out.println("For [REDACTED]'s sake, add an activity.");
} else {
for (String listItem: toDoList) {
System.out.println(listItem);
}
}

}

public void deleteItem() {
System.out.println("Please choose the number of the line you want to delete:");
displayList();
int userinput = inputread.nextInt();
int listPos = userinput - 1;
toDoList.remove(listPos);
System.out.println("That item has been deleted. Type any key and press Enter to continue.");
String discardMe = inputread.next();
menu();
}
}

最佳答案

我建议使用 BufferedReader 而不是 Scanner 类。 Scanner 的问题在于它会查找空格和换行之间的标记,因此当您添加诸如 Go to the store 之类的内容时,空格之间的每个标记都会被拾起后,您最终会前往 前往 商店,而不是 1 个大代币。您可以通过使用 BufferedReader 声明它来获取输入:

public static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));

然后,在您的 addItem() 方法中,在 while(true) 循环中,您从阅读器读取输入,然后检查它是否为空。如果它为空,则中断循环并退出该函数,否则将一个项目添加到列表中。

    System.out.println("Please type the item to add to the To-Do List"); // Output
while (true) { // Continue adding items until user just hits enter
String newItem = buf.readLine(); // read user input
if (newItem == null || newItem.isEmpty()) { // check if the user entered anything, or just hit enter
break; // If they didn't enter anything, then break the loop and drop out of the function
}
toDoList.add(newItem); // if they did enter something, add it to your to-do list

}

例如,为了测试这个,我使用了一个 main 方法:

public static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {
List<String> toDoList = new ArrayList<String>();
System.out.println("Please type the item to add to the To-Do List");
while (true) {
String newItem = buf.readLine();
if (newItem == null || newItem.isEmpty()) {
break;
}
toDoList.add(newItem);

}
System.out.println("Your item has been added! Type any key and press Enter to continue");

for (String s : toDoList) {
System.out.println(s);
}
}

然后,当提示输入时,我输入:

Please type the item to add to the To-Do List
Go to the grocery store and get milk
Stop by the gym and pay membership fees
Pick up flowers for the wife

对于输出我得到:

Your item has been added! Type any key and press Enter to continue
Go to the grocery store and get milk
Stop by the gym and pay membership fees
Pick up flowers for the wife

关于java - 如何将多个单词存储为 ArrayList 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28069703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com