Code doesn't wait for user input?

2019-02-26 14:40发布

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 client can instantiate a book with a title and author
  • The client can examine but not modify a book's title or author
  • The client can ask a patron wthere it has borrowed a given book (identified by title).
  • The client can tell a patron to return a given book (identified by title).
  • The client can tell a patron to borrow a given book.

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.

4条回答
唯我独甜
2楼-- · 2019-02-26 15:02

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.

查看更多
疯言疯语
3楼-- · 2019-02-26 15:03

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.

查看更多
不美不萌又怎样
4楼-- · 2019-02-26 15:10

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

查看更多
forever°为你锁心
5楼-- · 2019-02-26 15:19

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

查看更多
登录 后发表回答