ITGuru
ITGuru

Reputation: 117

Boolean loop wont set false

i have loops in all my other applications, but this just wont seem to work.

public void loanBook() {
    boolean loop3 = true;
    System.out.println("So you wanna loan a book? Excellent choice.");
        while (loop3) {
            for (int i=0; i<books.size(); i++) {

                System.out.println("Search for the book you're looking for: ");
                String book = in.nextLine();

                if (books.get(i).toString().contains(book) == true) {
                    System.out.println("Looking for: " + books.get(i).toString() + "?");
                    System.out.println("press y to loan book or n to try again");
                    String choice1 = in.next();
                    if (choice1.equalsIgnoreCase("y")) {
                        for (int j=0; j<customers.size(); j++) {
                            if (customers.get(j).getSignedIn() == true) {
                                customers.get(j).booksLoaned.add(books.get(i)); 
                                Timer.delayFunction();
                                System.out.println(customers.get(j).printBookList()); 
                                System.out.println("Returning to main menu");
                         ---->   loop3 = false;  doesnt work for some reason       
                            }
                        }
                    }
                }
            } 
        System.out.println("Couldnt find book");
        }
    } 

It keeps repeating the loop3 even though i set it to false. It should go back to the main menu afterwards, but it wont.

I tried to isolate the problem, and no problem. I've been looking at the code for an hour now and i cant seem to find any problem. Anybody got a clue?

the isolated problem that worked:

public void loanBook() {
    boolean loop = true;
    while (loop) {
        System.out.println("lol");
 ----->   loop = false;   works fine
    }
    System.out.println("lol");
}

my System out prints work just fine so i know it gets through my loops correctly.

Upvotes: 1

Views: 223

Answers (2)

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37594

You want to break out of the while loop as soon as you set it to false. You could do that with labels

public void loanBook() {
    boolean loop3 = true;
    System.out.println("So you wanna loan a book? Excellent choice.");
        while (loop3) {
            outerloop: // added
            for (int i=0; i<books.size(); i++) {

            System.out.println("Search for the book you're looking for: ");
            String book = in.nextLine();

            if (books.get(i).toString().contains(book) == true) {
                System.out.println("Looking for: " + books.get(i).toString() + "?");
                System.out.println("press y to loan book or n to try again");
                String choice1 = in.next();
                if (choice1.equalsIgnoreCase("y")) {
                    for (int j=0; j<customers.size(); j++) {
                        if (customers.get(j).getSignedIn() == true) {
                            customers.get(j).booksLoaned.add(books.get(i)); 
                            Timer.delayFunction();
                            System.out.println(customers.get(j).printBookList()); 
                            System.out.println("Returning to main menu");
                             loop3 = false;
                             break outerloop; // added     
                        }
                    }
                }
            }
        } 
    System.out.println("Couldnt find book");
    }
} 

Upvotes: 0

Eran
Eran

Reputation: 393781

You have 3 nested loops. The other loop (the while loop) terminates when loop3 becomes false.

However, you are setting loop3 to false deep within the inner most loop, which means the while loop won't check the value of loop3 until the 2 inner for loops finish.

If you want to break from all the loops, you'll need some break statements and or additional conditions. For example:

    while (loop3) {
        for (int i=0; i<books.size() && loop3; i++) {

            System.out.println("Search for the book you're looking for: ");
            String book = in.nextLine();

            if (books.get(i).toString().contains(book)) {
                System.out.println("Looking for: " + books.get(i).toString() + "?");
                System.out.println("press y to loan book or n to try again");
                String choice1 = in.next();
                if (choice1.equalsIgnoreCase("y")) {
                    for (int j=0; j<customers.size() && loop3; j++) {
                        if (customers.get(j).getSignedIn()) {
                            customers.get(j).booksLoaned.add(books.get(i)); 
                            Timer.delayFunction();
                            System.out.println(customers.get(j).printBookList()); 
                            System.out.println("Returning to main menu");
                            loop3 = false;    
                        }
                    }
                }
            }
        } 
        System.out.println("Couldnt find book");
    }

This will leave both inner loops when loop3 becomes false, which will allow the outer loop to terminate immediately.

Upvotes: 1

Related Questions