Java personal library


#1

The error notification is :

Library.java:13: error: cannot find symbol
for (String book : library.KeySet()) {
^
symbol: method KeySet()
location: variable library of type HashMap<String,Boolean>
1 error

And the code is following:

import java.util.HashMap;

public class Library{
  public Library(){
    
  }
  
  public void getFinishedBooks (HashMap<String, Boolean> library){ 
    if(library.size() < 1){
      System.out.println("Bye");   
    }
    else {
      for (String book : library.KeySet()) {
        if (library.get(book) == true) {
          System.out.println(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);
        
        
    }
}

I double checked my result by compared with video solution, I found we did almost same, I would like to know what’s wrong with my code?


#2

Simple :slight_smile:
The keySet() method is case sensitive.
Rather than for (String book : library.KeySet()) { you should use for (String book : library.keySet()) {

Also, just another pointer, library.get(book) returns true, so you can avoid the extra == true in this case