user1340073
user1340073

Reputation: 45

Code doesn't wait for user input?

I have to do a project for my Computer Science class. The problem is:

Patrons of a library can borrow up to three books. A patron, therefore, has a name and up to three books. A book has an author and a title. Design and implement two classes, Patron and Book, to represent these objects and the following behavior:

The Patron class should use a seperate instance variable for each book (a total of three). Each of these variables is initially null. When a book is borrowed, the patron looks for a variable that is not null. If no such variable is found, the method returns false. If a null variable is found, it is reset to the new book and the method returns true. Similar considerations apply to other methods. Use the method aString.equals(aString) to compare two strings for equality. Be sure to include appropriate toString methods for your classes and test them with a tester program.

Here is my Client class, which contains the main method: http://pastebin.com/JpxCT2F6

Now my problem is that when I run the program, the program doesn't wait for user input. Here is what comes up in the console of Eclipse:

Please enter title of book 1: 
s
Please enter author of book 1: 
e
Please enter title of book 2: 
f
Please enter author of book 2:
t
Please enter title of book 3: 
g
Please enter author of book 3:
d
Which book would you like to check for?
s
The patron has taken out the book s
Would you like to return a book? (1 yes or 2 no)
1
Which book would you like to return?
Sorry, could not find the book 
Would you like to take out a book? (1 yes or 2 no)
2
Invalid option
Which book would you like to check for?
The patron does not have  taken out
Would you like to return a book? (1 yes or 2 no)

Ass you can see, the console doesn't wait for user input after "Which book would you like return?" Instead, it takes a blank value. And later in the code, i put in "2", which means to return no book, but instead gives me an invalid input output.

Upvotes: 2

Views: 1596

Answers (4)

Tabias
Tabias

Reputation: 5

You can use nextInt(); to cause the the input to stop and wait for a response.

Upvotes: 0

jmderes
jmderes

Reputation: 11

You just have to go to the next line. input.nextLine();

Upvotes: 1

NominSim
NominSim

Reputation: 8511

You use nextInt() on line 71 of your code, which gets the integer answer the user provides. Then you use nextLine() which Advances this scanner past the current line and returns the input that was skipped.. The input that is skipped is only the newline character from the previous nextInt() call (It doesn't read the whole line only the int).

You can skip this by calling input.nextLine() once before you want the input, or by using nextLine() instead of nextInt() and converting the string to the integer value.

Upvotes: 1

Steve H.
Steve H.

Reputation: 6947

nextLine eats the newline character. nextInt leaves it in the input buffer, and the next readLine terminates immediately.

Quick fix: use readLine for everything, then parse the int from string read.

Upvotes: 1

Related Questions