【问题标题】:Skipping An Input line in java在java中跳过输入行
【发布时间】:2013-02-14 01:25:08
【问题描述】:

所以我有一个 addItem() 方法,我可以在其中将书籍、磁带或 cd 对象添加到我的库存中。为了存储书籍、磁带或 cd 对象,我从用户那里获取对象的标题、作者和价格。但是,当我运行程序时,它会跳过“请输入标题:”并向右转到“请输入作者:”。然后当我让它显示信息时,标题字段为'null'。

static void addItem(int type) {     

    String title;
    String author;
    Double price;

    System.out.println("Please enter the title");
    title = keyboard.nextLine()

    System.out.println("Please enter the author");
    author = keyboard.nextLine();

    System.out.println("Please enter the price");
    price = keyboard.nextDouble();


        if(type == 0){
            Book BookStoreItem;

                BookStoreItem = new Book(title, author, price);

            System.out.println("You've entered:");
            System.out.println("Title: " + BookStoreItem.getTitle());
            System.out.println("Author: " + BookStoreItem.getAuthor());
            System.out.println("Price: " + BookStoreItem.getPrice());
        }

        if(type == 1){
            Tape BookStoreItem;

            BookStoreItem = new Tape(title, author, price);

            System.out.println("You've entered:");
            System.out.println("Title: " + BookStoreItem.getTitle());
            System.out.println("Author: " + BookStoreItem.getAuthor());
            System.out.println("Price: " + BookStoreItem.getPrice());
        }

        if(type == 2){                  
            CD BookStoreItem;

            BookStoreItem = new CD(title, author, price);

            System.out.println("You've entered:");
            System.out.println("Title: " + BookStoreItem.getTitle());
            System.out.println("Author: " + BookStoreItem.getAuthor());
            System.out.println("Price: " + BookStoreItem.getPrice());
        }


}//end of addItem()

这是一个示例输出:

Press:
    0    To add a book to the inventory
    1    To add a tape to the inventory
    2    To add a Cd to the inventory
Please enter your choice: 0
Please enter the title:
Please enter the author: John Smith
Please enter the price: 55.50

Title: null
Author: John Smith
Price: 55.5

这里是菜单的更新:

static void printMenu() {
    System.out.println("\nPress:");
    System.out.println("\t" + ADD_BOOK + "\tTo add a book to the inventory\n");
    System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the inventory\n");
    System.out.println("\t" + ADD_CD + "\tTo add a CD to the inventory\n");
    System.out.println("\t" + SHOW_ITEM_LIST + "\tTo display a list of available books\n");
    System.out.println("\t" + SEARCH_ITEM + "\tTo search for a specific book\n");
    System.out.println("\t" + QUIT + "\tTo exit\n");
}

还有选择:

static void runBookStoreApplication() {
    int userChoice = QUIT;
    String quitMessage = "\nThank you for using the BookStoreApplication!";
    String invalidOptionMessage = "\nInvalid option!"; 

    do{
        printMenu();
        userChoice = getUserChoice();

        switch (userChoice) {
            case ADD_BOOK:
                addItem(0);
                break;
            case ADD_TAPE:
                addItem(1);
            case ADD_CD:
                addItem(2);
            case SHOW_ITEM_LIST:
                showItemList();
                break;
            case SEARCH_ITEM:
                searchItem();
                break;
            case QUIT:
                System.out.println(quitMessage);
                break;
            default:
                System.out.println(invalidOptionMessage);
        }
    } while(userChoice != QUIT);

}//end of runBookStoreApplication

使用 getUserChoice:

static int getUserChoice() {
    int choice;

    System.out.print("Please enter your choice: ");
    choice = keyboard.nextInt();

    return choice;
}

【问题讨论】:

标签: java input java.util.scanner output


【解决方案1】:

您没有向我们展示您询问“请输入您的选择:”的代码,但我敢打赌您使用的是 keyboard.nextInt() 或其他东西。此方法不会消耗所有行,只消耗整数0,因此当您随后调用nextLine() 时,它实际上会立即返回该行的其余部分(即使用空字符串)。

要解决这个问题,请在您的方法之前调用keyboard.nextLine()

编辑:

现在您已经输入了getUserChoice() 方法,您可以看到有一个对keyboard.nextInt() 的调用,正如我推测的那样。只需在它之后调用nextLine(),这样您就可以使用所有线路并且扫描仪已准备好进行下一次输入:

static int getUserChoice() {
    int choice;

    System.out.print("Please enter your choice: ");
    choice = keyboard.nextInt(); // consume just the integer

    // consume the rest of the line
    keyboard.nextLine();

    return choice;
}

【讨论】:

  • 好的,我更新了代码。我不认为我有 nextInt() 在那里?
  • getUserChoice() 中有什么内容?
  • @user1368970 你有nextDouble()
  • nextDouble() 在发布的代码中是有问题的代码之后。
  • 好的,我添加了 getUserChoice()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
相关资源
最近更新 更多