Help on Hashmaps

M

Marilyn Hart

Hi

I hope there is someone out there that may be able to help me.

I have a Hashmap called that contains Bank Account objects,
it is made up as follows.

Bank Acc Number - int
Bank Acc Type - String
Cust Number - int
Cust Name -String
Balance - double

I am trying to create an Iterator that will search each object in turn and
pass the Acc Type and Balance into a calculate Interest Method in another
class which will return the interest due. Then output on a simple output
dialog.

This is the code I have but it keeps giving me a Null Pointer Exception
error.

Where am I going wrong?

Iterator i = accList.keySet().iterator();
while (i.hasNext())
{
TypesOfAccount tempAcc = ((TypesOfAccount)accList.get(i));
accType = tempAcc.getType(); //String
balance = tempAcc.getBalance(); //double
interest = tempAcc.calcInterest(accType, balance);
total += interest; //double
}
JOptionPane.showMessageDialog(null,
"Interest on all accounts is " + total,
"Interest on All Accounts.",
JOptionPane.OK_OPTION);

What are the differences between keySet, valueSet and entrySet?
Which should I be using?

TIA

Paul
 
R

Ryan Stewart

message [...]
This is the code I have but it keeps giving me a Null Pointer Exception
error.

Where am I going wrong?

Iterator i = accList.keySet().iterator();
while (i.hasNext())
{
TypesOfAccount tempAcc = ((TypesOfAccount)accList.get(i));
accType = tempAcc.getType(); //String
balance = tempAcc.getBalance(); //double
interest = tempAcc.calcInterest(accType, balance);
total += interest; //double
}
JOptionPane.showMessageDialog(null,
"Interest on all accounts is " + total,
"Interest on All Accounts.",
JOptionPane.OK_OPTION);

What are the differences between keySet, valueSet and entrySet?
Which should I be using?
"accList.get(i)" will return the value in the HashMap to which the Iterator i is
mapped. Unless you're doing something *very* unusual, I'd give you 100:1 odds
that there is no such key in your Map. You want to replace that line (the first
one in the loop) with "TypesOfAccount tempAcc = (TypesOfAccount) i.next();". As
a side note, you might want to rethink your naming. TypesOfAccount and tempAcc
don't mean anything to me, at least.

Read the HashMap docs for the specifics of the methods. Briefly, keySet returns
a Set of all the keys in the map, entrySet returns a Set of all the entries
(each entry is of type Map.Entry), and valueSet doesn't exist. The values
method, however, returns a Collection of all the values.

Finally, please do not post to comp.lang.java. That group is deprecated.
Followups set to c.l.j.p.
 
B

Bjorn Abelli

...
I hope there is someone out there that may be able to help me.

I have a Hashmap called that contains Bank Account objects,
it is made up as follows.

Bank Acc Number - int
Bank Acc Type - String
Cust Number - int
Cust Name -String
Balance - double

1. How are you storing these Account objects in your HashMap?

2. What do you use as Key and Value, respectively?
I am trying to create an Iterator that will search each
object in turn and pass the Acc Type and Balance into a
calculate Interest Method in another class which will
return the interest due. Then output on a simple output
dialog.

This is the code I have but it keeps giving me a Null
Pointer Exception error.

3. What line in the code does that error message refer to?
Where am I going wrong?

On a lot of places...

First you get a set of "keys", from which you fetch an "iterator".
Iterator i = accList.keySet().iterator();

You iterate through the keyset...
while (i.hasNext())
{

....but tries to use the *iterator* as a *key* to your hashmap?
TypesOfAccount tempAcc = ((TypesOfAccount)accList.get(i));


To get the *key* from the keyset, you should use i.next, but in this case
I'm not even sure that is enough, as I don't believe that you have used the
actual accounts as keys?

4. Is "TypesOfAccount" the class for Accounts?
What are the differences between keySet, valueSet and entrySet?

- keySet returns a Set of the keys in the HashMap
- entrySet returns a Set of the entries (key-value pairs)
- values returns a Collection of the values in the HashMap

http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html

Which should I be using?

That depends on how you actually have stored the values in the HashMap, and
what you have been using as key and value respectively.


Say that you have stored them in this way, or similar:

accList.put(new Integer(acc.getBankAccNumber(), acc);

As you state that you will search "each object in turn", one suggestion
would then be to use "values".

Collection c = accList.values();

....and iterate that:

Iterator i = c.iterator();

while (i.hasNext())
{
TypesOfAccount tempAcc = (TypesOfAccount) i.next();
accType = tempAcc.getType();
balance = tempAcc.getBalance();
interest = tempAcc.calcInterest(accType, balance);
total += interest;
}

That is, if the TypesOfAccount really is the class for your accounts, and it
has the methods getType, getBalance and calcInterest...

// Bjorn A
 
R

Ryan Stewart

Ryan Stewart said:
You want to replace that line (the first one in the loop) with "TypesOfAccount
tempAcc = (TypesOfAccount) i.next();".

Of course I meant:
TypesOfAccount tempAcc = (TypesOfAccount) accList.get(i.next());
 
M

Marilyn Hart

----- Original Message -----
From: "Bjorn Abelli" <[email protected]>
Newsgroups: comp.lang.java.programmer
Sent: Saturday, January 22, 2005 2:51 PM
Subject: Re: Help on Hashmaps

...


1. How are you storing these Account objects in your HashMap?

2. What do you use as Key and Value, respectively?


3. What line in the code does that error message refer to?


On a lot of places...

First you get a set of "keys", from which you fetch an "iterator".


You iterate through the keyset...


...but tries to use the *iterator* as a *key* to your hashmap?



To get the *key* from the keyset, you should use i.next, but in this case
I'm not even sure that is enough, as I don't believe that you have used the
actual accounts as keys?

4. Is "TypesOfAccount" the class for Accounts?


- keySet returns a Set of the keys in the HashMap
- entrySet returns a Set of the entries (key-value pairs)
- values returns a Collection of the values in the HashMap

http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html



That depends on how you actually have stored the values in the HashMap, and
what you have been using as key and value respectively.


Say that you have stored them in this way, or similar:

accList.put(new Integer(acc.getBankAccNumber(), acc);

As you state that you will search "each object in turn", one suggestion
would then be to use "values".

Collection c = accList.values();

...and iterate that:

Iterator i = c.iterator();

while (i.hasNext())
{
TypesOfAccount tempAcc = (TypesOfAccount) i.next();
accType = tempAcc.getType();
balance = tempAcc.getBalance();
interest = tempAcc.calcInterest(accType, balance);
total += interest;
}

That is, if the TypesOfAccount really is the class for your accounts, and it
has the methods getType, getBalance and calcInterest...

// Bjorn A
Bjorn Thanks very much for that the part creating the collection and
iterating that worked perfectly.

I now need to work out the highest and lowest earning accounts.

The highest works fine. but the lowest keeps returning a non existant
account with zero interest.

This is what I tried

int tempNum = latestAccNum; //this is the newest account
number (I also tried 0)
Collection accounts = accList.values();
Iterator i = accounts.iterator();
while (i.hasNext())
{
TypesOfAccount tempAcc = (TypesOfAccount)i.next();
accNum = tempAcc.getAccNum();
accType = tempAcc.getType();
balance = tempAcc.getBalance();
interest = tempAcc.calcInterest(accType, balance);

if (interest < lowest)
{
lowest = interest;
tempNum = accNum;
}
else
{
}
}
JOptionPane.showMessageDialog(null,
"Lowest Interest due on account " + tempNum + " is "+
myFormat.format(highest),
"Interest on Account.",
JOptionPane.OK_OPTION);
}
 
B

Bjorn Abelli

I now need to work out the highest and lowest
earning accounts.

The highest works fine. but the lowest keeps
returning a non existant
account with zero interest.

From your code, I don't even see how the "highest" would work either...

I guess that you want to show the account number of both.

The iteration as such looks good, but you need *two* temporary variables for
*each* of the cases (highest, and lowest).

// It doesn't matter what the temporary account
// numbers are initialized to

int lowNum = 0;
int highNum = 0;

// But it does for the temporary interest variables!

double lowest = Double.MAX_VALUE;
double highest = Double.MIN_VALUE;

[As I said, the iteration looks okej, so I skip that here...]

// My guess is that you initialized "lowest" to 0, which
// means that you probably won't get any lower...
// The initialization above is one possible solution...

if (interest < lowest)
{
lowest = interest;
lowNum = accNum;
}

if (interest > highest)
{
highest = interest;
highNum = accNum;
}

There are more things to say about your code, such as what to do if there
are no accounts, but that should be another excercise.

As these things are quite fundamental and basic for programming, not only in
Java, perhaps you should consider the group comp.lang.java.help instead,
which is more aimed for beginners questions.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

NewBee help with HashMaps 1
NewBee help with HashMaps - extra 10
Help with a on numeric value encountered warning 2
Lexical Analysis on C++ 1
School Project 1
Student Banking Problem 10
TF-IDF 1
How to fix this code? 1

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top