0

I have an array list containing books. What I am trying to do (for various reasons) is use a While loop to keep on adding books to my array list until I tell the program to stop. In order to get it to stop, I am using a Scanner to ask the human user if another book should be added. If the user says yes, then the method would repeat, otherwise, the method will stop.

Unfortunately, I am stuck in 2 areas (for now). First, I am not sure how to insert the decision into the method, I tried:

while(decision.equals("Yes"){addBooks(anyTitle,anyAuthor);},

however, that just seemed wrong as it goes into a loop (while I want), however it causes 2 issues: A)Even if I type something else, the method does not end (unless I use another scanner and include a break), and B) The same inputted fields get inserted into the array list over and over again, without giving me a chance to input new Strings.

/**
 * Method to add a single book
 */
public void setOneBook(String anyTitle, String anyAuthor){
    bookList.add (new Book(anyTitle,anyAuthor));
}

/**
 * Method to add books to collection until the command to stop is given.
 */
public void addBooks(String anyTitle, String anyAuthor){
    bookList.add (new Book(anyTitle,anyAuthor));
    System.out.println("Would you like to add another book?");
    String decision = keybd.next();
}

I'm feeling pretty dumb at the moment, so please keep any snide comments to yourself.

While this is up, I will be messing around with my code, so if anything changes (for better or for worse), I will update my post.

2
  • The decision you declare inside addBooks() is not the same variable as the one controlling your loop, because in Java everything is passed by value. Commented Feb 24, 2015 at 3:29
  • What you're trying to do sounds very similar to what I've proposed here. Commented Feb 24, 2015 at 3:52

2 Answers 2

5

Separate the logic of asking the question for more information from that of updating the list

String decision = "No";
do {
    System.out.println("Would you like to add another book?");
    decision = keybd.nextLine();
    if (decision.equals("Yes")) {
        // Get information about book
        String anyTitle = keybd.nextLine();
        String anyAuthor = keybd.nextLine();
        addBooks(anyTitle,anyAuthor);
    }
} while (decision.equals("Yes"));

You could go a step further, making a addBook method which prompts the user for the informaiton...

public void addBook() {
    String anyTitle = keybd.nextLine();
    String anyAuthor = keybd.nextLine();
    addBooks(anyTitle,anyAuthor);
}

Then you could just call addBook from the while-loop

do {
    System.out.println("Would you like to add another book?");
    String decision = keybd.nextLine();
    if (decision.equals("Yes")) {
        addBook();
    }
} while (decision.equals("Yes"));

Further decoupling your code, allowing you to call addBook whenever you want to prompt the user for information about the book and adding it to the list

Sign up to request clarification or add additional context in comments.

2 Comments

Three questions about the first part of your answer: 1) Based on what you suggested, the decisions variable is not able to be found. I tried a few things, but they did not help. 2) If the method name is : public void addBooks(String anyTitle, String anyAuthor) then would they even need to be identified as Strings again in the coding? 3) Even putting aside the other two questions, when I do get it to work, it does not give me any place to enter information for the book even though it shows that the program is running.
1- decision should be declared before the do loop starts, corrected; 2- Yes, because the parameters to the method only have context/meaning within the method, to the rest of the code, they are just place holds through which you pass information; 3- Then the condition which allows the user to enter the book information isn't been met, remember, equals is case sensitive, so you must enter "Yes" exactly
2

If I understand your various snippets of code, I would strongly recommend you consider something that calls your method setOneBook... but I suggest you change the method visibility (and name) -

private void addOneBook(String anyTitle, String anyAuthor){
    bookList.add (new Book(anyTitle,anyAuthor));
}

This way you have used encapsulation to hide the method that adds a book. Next, change the public method. You are adding multiple books, so don't pass in a title or an author -

public void addBooks() {
    do {
        String anyTitle = keybd.nextLine();
        String anyAuthor = keybd.nextLine();
        addOneBook(anyTitle, anyAuthor);
        System.out.println("Would you like to add another book?");
    } while (keybd.next().equalsIgnoreCase("yes"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.