-2

I'm working on a Library Application which has an edit book method, but currently I'm having problems with getting it to do what I want to to. Here's what I have so far;

The editBook() method;

public void editBook(int ID, String title, String author, int quantity, int numOnLoan, int numTimesLoaned)  {
        int i = 0;
        this.myLibrary.get(i).setTitle(title);
        this.myLibrary.get(i).setAuthor(author);
        this.myLibrary.get(i).setQuantity(quantity);
        this.myLibrary.get(i).setNumOnLoan(numOnLoan);
        this.myLibrary.get(i).setNumTimesLoaned(numTimesLoaned);
//The new details of the book are printed out below to confirm the details that the user has entered        
        System.out.println("The new details of this book are: " + "ID: " + this.myLibrary.get(i).getID() + 
                "Title: " + this.myLibrary.get(i).getTitle() + " Author: " + 
        this.myLibrary.get(i).getAuthor() + " Quantity: " + this.myLibrary.get(i).getQuantity() + " No. on Loan: " +
        this.myLibrary.get(i).getNumOnLoan() + " No. of times loaned: " + this.myLibrary.get(i).getNumTimesLoaned());    

Then here is the code in the switch statement

case 2: //Edit a Book

            System.out.println("You have chosen to edit a book.");
            System.out.println("Enter the ID of the book you would like to edit: ");
            int ID = sc.nextInt();
            int i = ID;

            System.out.println("You are about to edit: " + mcClay.getBooks().get(i).getTitle() + " by " + 
                    mcClay.getBooks().get(i).getAuthor() );

            System.out.println("Please enter the book's new title: ");
            title = sc.nextLine();
            sc.next();

            System.out.println("Please enter the book's new author: ");
            author = sc.nextLine();
            sc.next();

            System.out.println("How many books do you have?: ");
            while (!sc.hasNextInt()) {
                System.out.println("Enter an integer!");
                sc.nextLine();
                }
            quantity = sc.nextInt();

            System.out.println("How many books currently on loan? (Enter 0 if none): ");
            while (!sc.hasNextInt()) {
                System.out.println("Enter an integer!");
                sc.nextLine();
                }
            numOnLoan = sc.nextInt();

            System.out.println("How many times has this book been loaned out? (Enter 0 if never): ");
            while (!sc.hasNextInt()) {
                System.out.println("Enter an integer!");
                sc.nextLine();
            }
            numTimesLoaned = sc.nextInt();

I tested it with the following inputs and this is what I got;

You have chosen to edit a book.
Enter the ID of the book you would like to edit: 
1
You are about to edit: Harry Potter and the Chamber of Secrets by J.K.     Rowling
Please enter the book's new title: 
Harry Potter
Please enter the book's new author: 
J.K Rowling
How many books do you have?: 
Enter an integer!
1
How many books currently on loan? (Enter 0 if none): 
0
How many times has this book been loaned out? (Enter 0 if never): 
0
The new details of this book are: ID: 0Title:  Author:  Potter  Quantity: 1 No. on Loan: 0 No. of times loaned: 0    

This is clearly wrong as neither the correct ID, Title or Author are being stored and the input for "quantity" throws an error even though I haven't entered anything.

I'm still new to programming and this has me really confused.

Spectose
  • 11
  • 1
  • 5
  • Bear in mind that you don't need to consume leftover newlines if you're reading via `nextLine()` - the newline is the delimiter in this instance and is consumed – JonK Dec 09 '15 at 16:56
  • Use `next` instead of `nextLine` to consume invalid tokens. `nextLine` tries to read data until next line separator or end of stream, but `nextInt` doesn't consume line separators. – Pshemo Dec 09 '15 at 17:03

1 Answers1

-3

Try creating separate Scanners for Strings and ints. The Java Scanner tends to exhibit strange behavior when utilizing it to read in multiple types.

sgabhart22
  • 38
  • 5