0

Hi I am trying to display the content of a hashset which has different data types within it. I am using a different method to display the hashset. All i get is [FVC.Book@35bbe5e8] or this sort of thing and i am wanting it to be displayed like this.

------------- Book --------------

Title is: it Authored By: steven king Book Date of Published is: 1942 ISBN is: 1474147414741 Number of Copies available: 3

This is current what i have

public void displayBooks() {

   String display = books.toString();

    System.out.println(display);

    displayMenu();
}

which gives me the the reference [FVC.Book@35bbe5e8].

I have also tried a for loop

for(Book book : books) System.out.println(book);

which also give me the FVC.Book@ reference.

HashSet for Book

public class FVC {

 static HashSet<Book> books;
 static HashSet<Member> members;
 static HashSet<Loans> loans;
 static HashMap<String, Integer> userBorrowed;
 Scanner in = new Scanner(System.in);
 String title, isbn, author, publishDate;
 int copies;

public FVC(String title, String isbn, String author, String 
     publishDate, int copies, boolean suspended,
        boolean borrowed) {
    super();
    books = new HashSet<Book>();
    members = new HashSet<Member>();
    loans = new HashSet<Loans>();
    
    login();
    addBook();
    displayMenu();
}
 } 

displayBook Method 2nd try

   public void displayBooks() {
    Iterator<Book> itr = books.iterator();
    while(itr.hasNext()) {
        System.out.println(itr.next());
    }
    displayMenu();}

Here is the rest of the code where i create the book through user input.

   public void addBook() {

    int qty;
    System.out.println("How many books you would like to add? ");
    qty = in.nextInt();
    for (int i = 0; i < qty; i++) {
        in.nextLine();
        System.out.println("Enter the Title of the Book.");
        title = in.nextLine();
        System.out.println("Enter the ISBN of the Book.");
        isbn = in.nextLine();
        System.out.println("Enter the Author of the Book.");
        author = in.nextLine();
        System.out.println("Enter the Publish Date of the Book.");
        publishDate = in.nextLine();
        System.out.println("Enter the No of Copies of the Book.");
        copies = in.nextInt();
        books.add(new Book(title, isbn, author, publishDate, 
        copies, false, false));
        System.out.println(toString());
     }
     displayMenu();
    }// end addBook

@Override
public String toString() {
    return "------------- Book --------------\n" + "Title is: " + this.title + "\n" + "Authored By: " + this.author
            + "\n" + "Book Date of Published is: " + this.publishDate + "\n" + "ISBN is: " + this.isbn + "\n"
            + "Number of Copies available: " + this.copies + "\n";
}

public void displayBooks() {

    String display = books.toString();
    System.out.println(display);
    displayMenu();
}

I have tried a fews things and i know i am missing something. The toString method that i made does work as it displays the book after i added it, i just cant get the same output when i use another method to display the books that just got added. I have also tried forEach which give a reference alsoi think i tried a streamOf() method also but still kept on getting the reference or it wasn't compatible with hashSets.

Please help :)

  • 1
    Please post `code` not images of code ... But maybe you are looking for [`string.Join`](https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=net-6.0) – derpirscher Jun 15 '22 at 14:09
  • I have edited my question, i have also tried . join and joining neither worked. My Hashset has Strings but 1 int which i am trying to display. – Alanna McKelvie Jun 15 '22 at 14:50
  • You (almost certainly) have not overridden the `toString()` method of `Book` – Bohemian Jun 15 '22 at 15:13

0 Answers0