0

The scanner works up until I get to the the part where I ask the user if they have read the book. When I type in yes or no it automatically just goes to the else case. Then when I try to see what the input was after I type in yes or no it prints out blank.

import java.util.Scanner;

public class YourBooks {

    public static void main (String [] args){
        String title,author,publisher,rating,dateS,dateF,dateP,ans;
        int isbn,pages; 
        Scanner keyboard=new Scanner(System.in);
        System.out.println("What is the title?");
        title=keyboard.nextLine();
        keyboard.nextLine();
        System.out.println("Who is the author?");
        author=keyboard.nextLine();
        System.out.println("How many pages does the book have?");
        pages=keyboard.nextInt();
        System.out.println("Who is the publisher?");
        publisher=keyboard.nextLine();
        keyboard.nextLine();
        System.out.println("What is the Isbn?");
        isbn=keyboard.nextInt();
        System.out.println("Have your read the book?");
        ans=keyboard.nextLine();
        keyboard.nextLine();
        if (ans.equalsIgnoreCase("yes")){
            System.out.println("When did you start reading the book?");
            dateS=keyboard.nextLine();
            System.out.println("When did you finish reading the book?");
            dateF=keyboard.nextLine();
            System.out.println("How would you rate the book?");
            rating=keyboard.nextLine();

        }
        else if (ans.equalsIgnoreCase("no")){
                System.out.println("When do you plan to read the book?");
                dateP=keyboard.nextLine();

        }
        else{
            System.out.println("I do not understand what you are trying to     say.\nEnd of program");
            System.exit(0);
        }

    }
}
SomeJavaGuy
  • 7,137
  • 2
  • 18
  • 33
  • `ans=keyboard.nextLine(); keyboard.nextLine();`, it seems that you´re reading the line seperator as you did `nextInt` before, store it in `ans`, and do an "empty",you´re not storing the result, `keyboard.nextLine()` afterwards, where you´d read the actual input. [read this for a few more info](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – SomeJavaGuy May 03 '17 at 05:10
  • `keyboard.nextLine();` ... you have a rogue call to `Scanner#nextLine()` here. I don't know why it is there, but it probably shoudn't be. – Tim Biegeleisen May 03 '17 at 05:11

0 Answers0