0

そのため、本、テープ、または CD オブジェクトをインベントリに追加できる addItem() メソッドがあります。本、テープ、または 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;
}
4

1 に答える 1

2

You didn't show us the code where you ask "Please enter your choice:", but I bet that you are using keyboard.nextInt() or something. This method does not consume all the line, just the integer 0, so when you call nextLine() afterwards it actually returns immediately with the rest of the line (that is, with an empty string).

To solve this, call keyboard.nextLine() before your method.

Edit:

now that you have put your getUserChoice() method, you can see that there is a call to keyboard.nextInt(), as I presumed. Just call nextLine() after it so you consume all the line and the scanner is ready for your next input:

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;
}
于 2013-02-14T01:28:19.800 に答える