Personal Library


#1
import java.util.HashMap;

public class Library{
 
  public Library(){
    
  }
  
  void getFinishedBookes(){
    
    HashMap<String, Boolean> library = new HashMap<String, Boolean>();
    
    if (library.size() < 1){
      System.out.println("Error. Not empty");
    } else {
      for (String book : library.keySet()){
        
        System.out.println(book);
        
        if (book == ????){ //  What is there to write to check the variable book for truth?
          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);
    
    
  }
  
}

#2

here you call the method:

myLibrary.getFinishedBooks(myBooks);

but if i look at your method deceleration:


  void getFinishedBookes()

no parameter, how is Java to know what to do with this argument then?

Once you included the parameter, look over the function again to see what you should do with the parameter.


#3

Suppose I do this: void getFinishedBookes (int myBooks) or can I write “int a” in brackets? How do I check the book for “true” or “false”?


#4

you can name the parameter anything you like, but the argument, myBooks, you pass a method call is not of type int. The data type of parameter has to match the data type of argument you supply at method call.

that is surprisingly tricky in java, thankfully stackoverflow comes to the rescue:


#5

Thank you so much. There was one mistake left. I could not fix it from the previous project.

import java.util.HashMap;

public class Library{

public Library(){

}

void getFinishedBookes(String a, Boolean b){

HashMap<String, Boolean> library = new HashMap<String, Boolean>();

if (library.size() < 1){
  System.out.println("Error. Not empty");
} else {
  for (String book : library.keySet()){
    
    System.out.println(book);
    
    if (book.equals(false)){
      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);

}

}

Library.java:38: error: cannot find symbol
myLibrary.getFinishedBooks(myBooks);
^
symbol: method getFinishedBooks(HashMap<String,Boolean>)
location: variable myLibrary of type Library
1 error


#6

Found. Typo. Here’s the final error

import java.util.HashMap;

public class Library{

public Library(){

}

void getFinishedBooks(String a, Boolean b){

HashMap<String, Boolean> library = new HashMap<String, Boolean>();

if (library.size() < 1){
  System.out.println("Error. Not empty");
} else {
  for (String book : library.keySet()){
    
    System.out.println(book);
    
    if (book.equals(false)){
      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);

}

}

Library.java:38: error: method getFinishedBooks in class Library cannot be applied to given types;
myLibrary.getFinishedBooks(myBooks);
^
required: String,Boolean
found: HashMap<String,Boolean>
reason: actual and formal argument lists differ in length
1 error


#7

you have 1 argument at method call, and 2 parameters.

lets say i promise you two give you 2 packages (parameters), and i only give you 1 package (argument) your reaction is very likely something like: where is the second package? Javas way of expression this, is throwing an error

why do you fail to realize the parameter also needs to be a hashmap? the argument is a hashmap, and i already told you, the argument at method call and parameter (at method deceleration) should match.


#8

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