Frog666
Frog666

Reputation: 15

Use a While loop to add to array list

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.

Upvotes: 0

Views: 3189

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

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

Upvotes: 5

Elliott Frisch
Elliott Frisch

Reputation: 201429

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"));
}

Upvotes: 2

Related Questions