I am hoping someone can help. I am new to Java so apologies if i have made a mess of the code.
I have an assignment due and I cant get the program to work.
The individual parts work but not together. info and code below:
2.Ask the user how many books their application should store
3.Add book details
4.Display details of a book and Display all the Books
Java program must have the following:
• A Book class with appropriate properties and methods (including get and set)
• Appropriate use of methods for the menu system
• Appropriate use of loops
• Appropriate use of a toString method in the Book class
• Appropriate use of validation of user entry
• Appropriate use of comments
• An application class that will run the menu described in the scenario
This doesn't help us help you. What exactly doesn't work?
Are you getting error messages? If so, post the *complete* error message.
Are you not getting the right results? If so, what input are you giving it.
one error on the book class is saying that the method is never used. not sure how to fix it.
all i want it to do is ask user how many books to store, user inputs details and the application stores then display all books and book details.
this is what I am getting!! hope you can help
How many books would you like to store?: 2
Enter the book title you want to remove: lehane
Amended book titles-nothing coming up
The Title of the Book: null
The Author of the Book: null
The Year of publish: 0
The name of the Publisher: null
The details of the book object is null
Enter the title of the Book 1: code
Enter the author of the Book 1: michael
Enter the Year of published 1:1988
Enter the name of the publisher1:obrian
then it displays book details:
Book Details 1:
The Title of the Book: code
The Author of the Book: michael
The Year of publish: 1988
The name of the Publisher:
Book Details 2:
After a quick read of your code I notice a couple of problems with your use of Scanner. You have other issues too, but this will trip you up.
On line 76 you get an int, this has a side effect of leaving a pending new-line in the queue waiting to be processed. Then on line 79 you get nextLine and this sees, "ah, ha! I've got a new-line", before you've even typed anything, because it was left over from nextInt. The result is 'publlsher' is an empty String. In order to fix this you need to flush the pending new-line immediately after you call nextInt(). You do this by calling nextLine() and ignoring the returned String.
The other trap you've fallen into is that a PROJECT should only ever open a single Scanner using System.in ONCE. You are opening it twice. The best way to enforce this is to make the Scanner a constant. As a public static constant it will be available to all classes and methods in your project.
Now replace all in.nextLine() with KEYBOARD.nextLine().
I would prefer to use a utility class for keyboard input. That class can hide the Scanner instance and also take care of that gotcha about nextLine(). You may not have the time to write a utility class, but if you search my posts for “Scanner utility class” you will eventually find enough parts to assemble a complete class (an one serious error).