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&#34 ;,则arrayList仅存储" Pick"。
我尝试使用上面的inputread.nextLine(),但后来我得到了一个" InputMismatchException"。有什么建议?我确定它很简单。
根据请求编辑以包括整个班级:
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();
}
}
答案 0 :(得分:1)
我建议使用BufferedReader
而不是Scanner
类。 Scanner
的问题在于它会在空格和新行之间查找标记,因此当您添加Go to the store
之类的内容时,白色空格之间的每个标记都会被拾取,最终会结束使用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
}
例如,为了测试这个,我使用了一个主方法:
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
答案 1 :(得分:1)
此代码中存在多个问题。
让我们首先解决您的输入问题。
在菜单中,您可以阅读一个数字。当您使用scanner.next()
,scanner.nextInt()
等时,它会读取以下项目 - 但不包括 - 任何空格或换行符。因此,空格或换行符保留在缓冲区中等待读取。
现在,当您转到addItem()
并使用nextLine()
时,它只会读取空格或换行符。如果它只是一个换行符(你按下 Return ),那么你得到一个空字符串,你可能不想将它添加到列表中。如果您使用next()
,它将跳过该换行符,但......它只会读取一个单词。
因此,您需要在菜单中的nextLine()
之后设置nextInt()
。读完整数后,您将清除缓冲区,包括换行符。
然后,在addItem()
方法中,您将能够再次使用nextLine()
,因为它现在将从一个全新的行开始 - 它将完整地读取下一行。
此外,discardMe
部分必须与nextLine()
合并,而不是next()
,否则不会清除下一行的结尾。
你的另一个问题是你没有问过的问题。你现在做的基本上是进入菜单,然后进入操作,然后进入菜单,然后进行操作。你不断调用越来越多的功能,你永远不会返回,或者更确切地说,当你显示列表时,你会从所有功能返回。
及时,这可能会导致堆栈溢出。
执行此操作的正确方法不是从操作方法内部调用menu()
,而是在显示菜单的menu()
方法中调用一个循环,调用相应的操作方法,当它返回(清除堆栈上的空间)时,循环回到菜单,依此类推。这样可以保持你的堆栈平整。
当然,您的菜单上应该有一个“退出”选项。
答案 2 :(得分:0)
Stultuskes的一个例子:
public void addItem() {
System.out.println("Please type the item to add to the To-Do List");
System.out.println();
try (Scanner in = new Scanner(System.in)) {
while (in.hasNext()) {
String newItem = in.next();
toDoList.add(newItem);
System.out
.println("Your item has been added! Type any key and press Enter to continue");
}
}
System.out.println(toDoList);
}
答案 3 :(得分:0)
public void addItem() {
System.out.println("Please type the item to add to the To-Do List");
//The skip gets over the leftover newline character
inputread.skip("\n");
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();
}
放入跳过为我修好了。我想感谢你们的答案。