Results 1 to 6 of 6
  1. #1
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default HashMap Troubles

    I'm trying to search a library program by book index and running into some trouble with my HashMap. Here's the code in question:
    Java Code:
        public String searchByBookIndex(int input) {
            NumberFormat fmt = NumberFormat.getCurrencyInstance();
            Map<Integer, Book> bookMap = new HashMap<>();        
            
            for (int i = 0; i < author.booksByAuthor.size(); i++) {
                bookMap.put(author.booksByAuthor.get(i).getBookIndex(), book);
            }
    
            if (bookMap.containsKey(input)) {
                Book books = bookMap.get(input);
                return "Search Results:" + "\n" + books.getTitle() + "     " + books.getGenre() + "     " + author.getName() + "     " + fmt.format(books.getPrice());
            } else {
                return "Nothing found with book index " + input;
            }
        }
    The search itself works fine; however, the problem is that the values of everything in the HashMap are overwritten by the last book entry. For example, let's say there are four books (index in parenthesis): The Book (1), A Wonderful Book (2), A Great Book (3), A Bad Book (4). Once the code above iterates, the HashMap out is this (via println): 1 = A Bad Book, 2 = A Bad Book, 3 = A Bad Book, 4 = A Bad Book. What's causing this?

    Any guidance is greatly appreciated.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: HashMap Troubles

    In line #6, you map each and every index value to the same 'book' object ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default Re: HashMap Troubles

    Well, I tried making it new Book() inside the put() method and also after the put() method...neither provides the results I need. If I put it within the put() method, the keys enter correctly still but the book itself is empty. If I put it after the put() method, the second book still overwrites the first book and the second is empty.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: HashMap Troubles

    Quote Originally Posted by Cod View Post
    Well, I tried making it new Book() inside the put() method and also after the put() method...neither provides the results I need. If I put it within the put() method, the keys enter correctly still but the book itself is empty. If I put it after the put() method, the second book still overwrites the first book and the second is empty.
    Probably the constructor new Book() creates an new empty book, so that explains it; each time you put a pair int, book in your map you have to pass it an appropriately populated book; your attempt was faulty.

    kind regards,

    Jos

    edit: or maybe the author.booksByAuthor() can give you the books you want; it's your program so you should know ...
    Last edited by JosAH; 02-09-2014 at 10:34 AM.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default Re: HashMap Troubles

    Actually, I just figured an "easier" way would be to add the books to the HashMap when they are read-in from the file. So far that seems to be working. Thanks for the nudges in the correct direction.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: HashMap Troubles

    Quote Originally Posted by Cod View Post
    Actually, I just figured an "easier" way would be to add the books to the HashMap when they are read-in from the file. So far that seems to be working. Thanks for the nudges in the correct direction.
    Good; I'm glad you figured it out yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  2. Clock Troubles
    By brycepower1 in forum New To Java
    Replies: 55
    Last Post: 05-12-2011, 12:33 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. Integer troubles again...
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 09-20-2009, 02:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •