Project: Personal Library


#1

The code seems to run fine, but it doesn't print anything. Can't seem to get my head around what's wrong here. Anyone? :smile:

import java.util.HashMap;

public class Library {

public Library() {
}

public void getFinishedBooks(HashMap<String, Boolean> Library) {
    if (Library.size() < 1) {
        System.out.println("HashMap is empty.");
    } else {
        for (String book : Library.keySet()) {
            if (Library.containsKey(true)) {
                System.out.println(Library.get(book));
            }
        }
    }
}

public static void main(String[] args) {
    HashMap<String, Boolean> myBooks = new HashMap<String, Boolean>();

    myBooks.put("Road Down The Funnel", true);
    myBooks.put("Rat: A Biology", false);
    myBooks.put("TimeIn", true);
    myBooks.put("3D Food Printing", false);

    Library myLibrary = new Library();
    myLibrary.getFinishedBooks(myBooks);
}

}


#3

I can't seem to find anyone with the same problem? Or anyone at all with topics about this project... Please help :slightly_smiling:


#4

Can you tell me what lesson number this is... And is this a Codecademy Pro Exclusive?


#5

It's the Pro yes. But i figured it out by myself. The getFinishedBooks method was wrong or the for each loop was. Instead of using

for (String book : Library.keySet()) {
if (Library.containsKey(true)) {
System.out.println(Library.get(book));
}
}

It should be:

for (String book: library.keySet()) {
if (library.get(book) == true) {
System.out.println("\nThis book has been completed: " + book);
}
}

That did the trick :smiley:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.