• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print out all account numbers from hashmap?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I call this method and run my program. It's only printing out one account number, when i'm trying to print out ALL the account numbers in my hashmap. Any tips?
Should I be using a for loop instead of a while? Essentially I'm trying to prevent the number from being overriden everytime the while loop iterates. I believe that is the issue

 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brian leung wrote:when i'm trying to print out ALL the account numbers in my hashmap.



I'm assuming that the "account number" is the String which is the key to the Map and that you don't have to get the account number from the Account which is the matching value.

But the code you posted is certainly not trying to print out all of the keys of the Map. You're only trying to print out the keys which contain the prefix, in a case-insensitive way. So you only get one account number: that would mean that only one account number contains the prefix.

And sorry, I couldn't understand the bit about "trying to prevent the number from being overridden", in particularly what you mean by "overridden" in that context. Oh! While I was typing that I realized that you might mean "overwritten". Still don't understand what that would mean either.
 
brian leung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Maybe I should've made this clearer.

Yes, the account number is the string which is the key to the HashMap. I am trying to print out ALL the accounts that START with the prefix, and yes in an case-insensitive way. What I meant by "overwritten" is. Inside the while loop, once the loop iterates through once, it prints out the "number" which I have set as i.next(). While that loop is true, i'm assuming, the next number in line is called and thus, that number is printed out, so like only printing out the last number and not ALL of the account numbers.

I hope that clears up any confusion?
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code will go through all of the String objects from the Iterator, yes. Is that different from what actually happened?
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brian leung wrote:I am trying to print out ALL the accounts that START with the prefix, and yes in an case-insensitive way.



Then String's "contain" method is the wrong one to use. There's a "startsWith" method.

And by the way your attempt at case-insensitive doesn't work, unless the prefix is only one character long. If the prefix is, say, "ab" then the code is checking to see if the account number contains "AB" or "ab". It doesn't check for "Ab" or "aB".
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want "AB", "ab", "Ab", and "aB", to be treated as the same I suggest coercing them to either upper case or lower case when you put the record into the map otherwise you'll end up with duplicate account numbers that only vary by case.
 
brian leung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

This code will go through all of the String objects from the Iterator, yes. Is that different from what actually happened?



It's still only printing out 1 account number.

In my main class. I have 3 customers created. I'll label them Tom, Bob, Jim.

All 3 of them have a Chequings and Savings account. The prefix for Chequings is "CH" and for Savings is "SA". Each customer will have a number followed by the account prefix to differentiate each customer account. For example.

Tom's chequing Account will be CH-123, and SA-123 for Savings.
Bob's chequing Account will be CH-456, and SA-456 for Savings.
Jim's chequing Account will be CH-789, and SA-789 for Savings.


When I call this method in my main class, and I put in "SA" as the prefix for the argument.

I ONLY get SA-789.  It doesn't print out SA-123, and SA-789. Same thing happens when I type in CH for the prefix. I ONLY get CH-789.

So I'm just wondering, am I iterating incorrectly? Is it going through the loop and then only printing out the most recent number, but not actually adding all the numbers and printing ALL of them out??
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That tells me that Paul was correct to suggest you only have one “K” in your Map.
Why did you put the @Override annotation on that method? What are you overriding? Why? That method looks like something that shouldn't be overriding anything.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brian leung wrote:I ONLY get SA-789.  It doesn't print out SA-123, and SA-789. Same thing happens when I type in CH for the prefix. I ONLY get CH-789.

So I'm just wondering, am I iterating incorrectly? Is it going through the loop and then only printing out the most recent number, but not actually adding all the numbers and printing ALL of them out??



You seem to be convinced that you have, what, six entries in that map? But the evidence says otherwise. The Map class has a size() method which tells you how many key-value pairs are in the Map, you could use that to see what it says.
 
brian leung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

brian leung wrote:I ONLY get SA-789.  It doesn't print out SA-123, and SA-789. Same thing happens when I type in CH for the prefix. I ONLY get CH-789.

So I'm just wondering, am I iterating incorrectly? Is it going through the loop and then only printing out the most recent number, but not actually adding all the numbers and printing ALL of them out??



You seem to be convinced that you have, what, six entries in that map? But the evidence says otherwise. The Map class has a size() method which tells you how many key-value pairs are in the Map, you could use that to see what it says.



Yeah. This seems to be the case that I only have my Customer with "789" currently in my map.

I tried running my program, and the code appears to work fine with that specific customer. I can deposit/withdraw and show transactions with that customer. However when I try to do it with the other 2 customers, "123" and "456". When I try to run these methods, all I get are NullPointerExceptions.
 
brian leung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That tells me that Paul was correct to suggest you only have one “K” in your Map.
Why did you put the @Override annotation on that method? What are you overriding? Why? That method looks like something that shouldn't be overriding anything.



The class that this function is in is implementing two other interfaces. So I'm essentially overriding the methods in the two other interfaces.


 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brian leung wrote:Yeah. This seems to be the case that I only have my Customer with "789" currently in my map.



My guess is that you're accidentally creating one Map per Customer in the other code, and then when you go to use the Map it's the one with the data for the last Customer.
 
brian leung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

brian leung wrote:Yeah. This seems to be the case that I only have my Customer with "789" currently in my map.



My guess is that you're accidentally creating one Map per Customer in the other code, and then when you go to use the Map it's the one with the data for the last Customer.



Ah I fixed it! I redesigned my main class and it appears to now be displaying all the account numbers in my class!

This brings me to my next question. If this function to display all codes works. How would I design it so that it only displays inactive accounts?
I've already created some inactive accounts by creating the accounts in my main class and then calling the setActive() functions and setting them to false.

Currently, when I run this code



All accounts including inactive ones get displayed.

I know I can use probably reuse this. But would I write this?

 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You wouldn't want to call "setActive(false)" to ask whether the account was active. I'm assuming you have a method which asks if the account is active, so you'd call that.

Also, now that you want something more than just the account number, that makes you have to get the whole account from the Map. That would work fine, but I'm (also) assuming that you can get the account number from the account using some method which is already defined. So it might be more straightforward to just iterate through the accounts, i.e. the values of the Map.
reply
    Bookmark Topic Watch Topic
  • New Topic