mardi 6 octobre 2015

If statement skips last line of user input code [duplicate]

This question already has an answer here:

I'm trying to figure out why chairArg4 = input.nextLine(); inside the else if statement is being skipped... I had a similar problem with bookArg4 = input.nextInt(); but I added a System.out.println(); immediately after that statement and it works... However when I tried adding System.out.println(); after chairArg4 = input.nextLine(); it still skips that line of code and no user input is saved...

Here is what the Chair output should look like:

Bookshelf

This is what it looks like:

Chair

My code:

if(chairOrBook.equals("Bookshelf")){
        System.out.println("Color: ");
        bookArg1 = input.nextLine();
        System.out.println("Height: ");
        bookArg2 = input.nextInt();
        System.out.println("Width: ");
        bookArg3 = input.nextInt();
        System.out.println("Number of shelves: ");
        bookArg4 = input.nextInt();
        System.out.println();
        Main book = new Bookshelf(bookArg1,bookArg2, bookArg3, bookArg4);
        System.out.println();
        book.chairShelf();
    } else if (chairOrBook.equals("Chair")){
        System.out.println("Color: ");
        chairArg1 = input.nextLine();
        System.out.println("Height: ");
        chairArg2 = input.nextInt();
        System.out.println("Width: ");
        chairArg3 = input.nextInt();
        System.out.println("Type of chair: ");
        chairArg4 = input.nextLine();
        System.out.println();
        Main chair = new Chair(chairArg1, chairArg2, chairArg3, chairArg4);
        System.out.println();
        chair.chairShelf();
    } else{
        System.out.println("Error");
    }

Aucun commentaire:

Enregistrer un commentaire