Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Beginning Java
Scanner debugging help
Haani Naz
Greenhorn
Posts: 23
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi there,
I have the following piece of code which fails at the point it needs to accept input and i can't seem to figure out why.
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); MyLibrary ml = new MyLibrary("Haani's Library"); //this won't print anything cos you haven't added any books ml.toString(); System.out.println("You have connected to " + ml.getName()+"..\n"); while(true){ //Main Menu System.out.println(); System.out.print("What would you like to do? \n" + "\n"+ "1. Add Books \n" + "2. Lend a book to a person \n" + "3. Check which Books are available \n" + "4. Exit \n" + "Enter choice [1 | 2 | 3 | 4]: "); String option = keyboard.nextLine(); if (option.equals("1")){ boolean cont=true; while(cont){ System.out.print("Enter name of Book: "); String bookName= keyboard.nextLine(); System.out.print("Enter name of the Author for " + bookName + ":"); String bookAuthor=keyboard.nextLine(); Book b1 = new Book(bookName); b1.setAuthor(bookAuthor); ml.addBook(b1); System.out.println("Would you like to add another book? [y/n]"); String choice = keyboard.nextLine(); if (choice.equals("y")){ cont=true; } else if (choice.equals("n")) { cont=false; } }//end-while }//end-if if(option.equals("2")){ boolean contd = true; //Print available books ArrayList<Book> availableBooks = ml.getAvailableBooks(); String input; //while (contd){ Person p1 = new Person(); System.out.print("Name of person lending book to: "); input = keyboard.nextLine(); p1.setName(input); System.out.print("What is the maximum number of books to lend: "); int maxBooks = keyboard.nextInt(); p1.setMaximumBoks(maxBooks); //add person to library ml.addPerson(p1); /* System.out.println("The available books are.."); for (Book book : availableBooks) { System.out.println(book); }*/ System.out.println("Which book would you like to check out to " + p1.getName()); input = keyboard.nextLine(); System.out.println(input); /******* //check if book name entered is in the database //if it is check it out - either successful or failed for (Book book : availableBooks) { if (book.title.equalsIgnoreCase(checkoutBook)){ boolean result =ml.checkOut(book, p1); if(result==true){ System.out.println("Check out: successful."); } else{ System.out.println("Check out failed: Book has already been checked out or " + p1.getName() + " has reached Maximum book limit"); } }//end-if: (book.title.equals("bookName")) else if (!(book.title.equalsIgnoreCase(checkoutBook))){ System.out.println("Book doesn't exist"); } }//end-for System.out.println("Would you like to check out another book? [y/n]"); String choice = keyboard.nextLine(); if (choice.equals("y")){ contd=true; } else if (choice.equals("n")) { contd=false; } }//end-while ******* */ }//end-if
The issue is at line 115.
Here is the output:
What would you like to do? 1. Add Books 2. Lend a book to a person 3. Check which Books are available 4. Exit Enter choice [1 | 2 | 3 | 4]: 1 Enter name of Book: War and Peace Enter name of the Author for War and Peace:Daffy Duck Would you like to add another book? [y/n] n What would you like to do? 1. Add Books 2. Lend a book to a person 3. Check which Books are available 4. Exit Enter choice [1 | 2 | 3 | 4]: 2 Name of person lending book to: Haani What is the maximum number of books to lend: 4 Which book would you like to check out to Haani What would you like to do? 1. Add Books 2. Lend a book to a person 3. Check which Books are available 4. Exit Enter choice [1 | 2 | 3 | 4]:
As you can see instead accepting input it skips it.
can somebody please tell me why?
thanks in advance!
Anniruddh Rana
Greenhorn
Posts: 18
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
System.out.println("Which book would you like to check out to " + p1.getName()); input = keyboard.nextLine();
Change that to
System.out.print("Which book would you like to check out to " + p1.getName()); input = keyboard.nextLine();
and give it a try.
i.e. use System.out.print there instead of System.out.println
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Help with Value Returning Methods
Library System
Do not know how to code some cases and exit the loop with "Enter"
how to compare if object exists in arraylist and add if doesn't
coin flip assignment help
More...