添加arrayList.set的用户输入

时间:2017-11-03 20:40:07

标签: java arrays

我一直在做以下任务:  该程序应该创建一个名为Book List的ArrayList。该程序 应该显示一个菜单,允许用户从以下选项中进行选择:

  

输入1将书籍添加到列表中:

     

输入2将书籍编辑到列表中:

     

输入3以从列表中删除图书:

     

输入4以显示书籍列表:

     

输入5退出:

程序应该使用case / switch语句并打开用户选择。该程序应该继续,直到用户输入5退出。

  1. 案例1应使用BookList.add将书籍添加到ArrayList。
  2. 案例2应使用BookList.set将书籍编辑到ArrayList。
  3. 案例3应使用BookList.remove从列表中删除名称(要求用户输入要从ArrayList中删除的书籍的索引号)。
  4. 案例4应该使用for循环来显示ArrayList中的所有书籍 以及他们的索引号。
  5. (示例输出)您的输出应类似于:

      

    **********图书清单*********

         

    索引:0名称:奇怪的土地中的陌生人

         

    索引:1名称:PHP和MySQL

         

    索引:2名称:HTML& CSS

         

    索引:3名称:爱情故事

         

    索引:4名称:地球静止的日子

    我遇到了ArrayList.set的问题。我无法弄清楚如何获取用户输入(索引号和更正的书名)来更新数组列表。这不是我对这个程序的问题的结束,但是对ArrayList.set的任何帮助都将不胜感激。

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class BookList {
    
        public static void main(String[] args) {
    
            Scanner keyboard = new Scanner(System.in);
            Scanner input = new Scanner(System.in);
    
            //Create array
            ArrayList<String> bookList = new ArrayList<String>();
    
            //Add a few books to the array bookList
            bookList.add("A Game of Thrones");
            bookList.add("A Clash of Kings");
            bookList.add("A Storm of Swords");
            bookList.add("A Feast for Crows");
            bookList.add("A Dance with Dragons");
    
            //Display the items in bookList array and their indices.
            System.out.println("******** The Book List ********");
            for (int index = 0; index < bookList.size(); index++)
            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
            System.out.print("\n");
    
    
            //declaring variables
            int menuID = 0;
            int changeIndex = 0;
            int delIndex = 0;
            String addTitle = "";
            String corrTitle = "";
    
    
            //Menu
            System.out.println("Enter 1 to add a book to the list");
            System.out.println("Enter 2 to edit a book in the list");
            System.out.println("Enter 3 to remove a book from the list");
            System.out.println("Enter 4 display the list of books");
            System.out.println("Enter 5 to quit");
            System.out.println("Enter a menu number (1-4 or 5 to Exit): ");
            menuID = input.nextInt();
    
            while (menuID != 0)
            {
    
                if (menuID >=1 && menuID <= 4)
                {
                    switch (menuID)
                    {
                        case 1:
                            System.out.println("Enter the book to add: ");
                            addTitle = keyboard.nextLine();
                            bookList.add(addTitle);
    
                            System.out.print("\n");
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 2:
                            System.out.println("Enter index number of the book to change: ");
                            changeIndex = keyboard.nextInt();
    
                            System.out.println("Enter the corrected book name: ");
                            corrTitle = keyboard.nextLine();
    
                            bookList.set(changeIndex, corrTitle);
    
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 3:
                            System.out.println("Enter index number of the book to remove: ");
                            delIndex = keyboard.nextInt();
                            bookList.remove(delIndex);
    
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 4:
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 5:
                            System.out.println("Goodbye!");
                            break;
                    }}
                else if (menuID >=1 && menuID <= 4)
                    System.out.println("You must enter a number 1-5:");
                System.out.println("Enter a menu number (1-4 or 5 to Exit): ");
                menuID = input.nextInt();
    
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

你应该只使用一个具有相同源的Scanner对象,除非你有一个非常复杂的方法来遍历输入,这可能不是这里的情况。所以你的代码应该是这样的:

// Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
// ...
menuID = input.nextInt();

while (menuID != 0)
{
    if (menuID >=1 && menuID <= 4)
    {
        switch (menuID)
        {
            case 1:
                // ...
                addTitle = input.nextLine();
// ...

现在,还有另外一个问题,因为你正在调用Scanner.nextInt,这会获得下一个整数,但在获取整数后不会消耗剩下的行,所以当您致电Scanner.nextLine时,它会为您提供上一行的其余部分,而不是您期望的新行。要解决此问题,您可以在每个Scanner.nextLine之后调用一个虚拟Scanner.nextInt,如下所示:

menuID = input.nextInt();
input.nextLine(); // Consumes the rest of the line

或者你可以每次都获得整行,并使用Integer.parseInt解析它包含的整数,如下所示:

menuID = Integer.parseInt(input.nextLine());

Here's the working code使用第二个选项(我觉得这个案例更好)

作为旁注,您要求用户退出5,但只要while不是menuID,您的0依赖就会继续