Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:53:17+00:00 2026-06-16T03:53:17+00:00

This is related to a question I asked earlier: Iterating through hashmap and creating

  • 0

This is related to a question I asked earlier:
Iterating through hashmap and creating unique objects – trying to prevent duplicates

And while I assumed I could apply a similar logic for my remove method that I had for my add method, the exception I have to check for a non existent record is getting thrown even though I know very well the record exists and should be removed. My delete method is as follows:

    public boolean removePatron(int libraryCardNumber) throws PatronException {
    boolean patronRemoved = false;
    int keyToRemove = 0;
    for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) {
        if (entry.getValue().getCardNumber() != libraryCardNumber) {
            throw new PatronException("This record does not exist");

        }
        keyToRemove = entry.getKey();
    }
    patrons.remove(keyToRemove);
    patronRemoved = true;
    return patronRemoved;
}

For reference, the Patron objects look like this:

public class Patron {

//attributes
private String name = null;
private int cardNumber = 0;

//operations
public Patron (String name, int cardNumber){
    this.name = name;
    this.cardNumber = cardNumber;
}

public String getName(){
    return name;

}

public int getCardNumber(){
    return cardNumber;
}

 }

My test simply adds three patrons first, and then tries to remove it by a card number that I know would exist. I added a println of the Patron’s number in my add method so I could see them easily while messing with this in eclipse as they get added.

    @Test
public void testRemovePatron() {
    boolean exceptionThrown = false;
    try {
        testLibrary.addPatron("TestName");
        testLibrary.addPatron("TestName2");
        testLibrary.addPatron("TestName3");
        testLibrary.removePatron(1);
    } catch (PatronException e) {
        System.out.println(e.getMessage());
        exceptionThrown = true;
        fail("what the hell is going on");
    }
    assertFalse(exceptionThrown);
}

I get the exception from the remove method thrown every time.

Edit: I made a small change to the provided answer, to account for needing to throw an exception if there was no match found:

    public boolean removePatron(int libraryCardNumber) throws PatronException {
    boolean patronRemoved = false;
    int keyToRemove = 0;
    for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) 
    {
        if (entry.getValue().getCardNumber() == libraryCardNumber) 
        {
            keyToRemove = entry.getKey();
            patronRemoved = true;
        }
    }
    if (patronRemoved)
    {
        patrons.remove(keyToRemove);
    } else {
        throw new PatronException("This record did not exist");
    }
    return patronRemoved;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:53:18+00:00Added an answer on June 16, 2026 at 3:53 am

    The exception will throw for any user who is NOT the one you are looking for.
    Change this:

    public boolean removePatron(int libraryCardNumber) throws PatronException{
        boolean patronRemoved = false;
        int keyToRemove = 0;
        for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) {
            if (entry.getValue().getCardNumber() != libraryCardNumber) {
                throw new PatronException("This record does not exist");
    
            }
            keyToRemove = entry.getKey();
        }
        patrons.remove(keyToRemove);
        patronRemoved = true;
        return patronRemoved;
    }
    

    to this:

    public boolean removePatron(int libraryCardNumber) {
        boolean patronRemoved = false;
        int keyToRemove = 0;
        for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) 
        {
            if (entry.getValue().getCardNumber() == libraryCardNumber) 
            {
                keyToRemove = entry.getKey();
                found = true;
            }
        }
        if (found)
        {
            patrons.remove(keyToRemove);
        }
        return patronRemoved;
    }
    

    or more concisely as AmitD showed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this is related to a question I asked earlier. I am trying to
This is related to this question I asked earlier about syntax highlighting user-defined blocks
This question is related to the one I asked here . I'm trying to
This question is related to this initial question asked a little while ago. Now,
This is related to an earlier question I asked, where the answer was: If
This is related to this question I asked a while back. The end game
This is related to an earlier question I asked. I'm adding a toString() method
This question is related to this question I asked a little while back. The
This question is related to my earlier question, asked here: How do I get
This question is related to one I asked earlier: Adding buttons to a TabControl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.