Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help on Hashmaps

Reply
Thread Tools

Help on Hashmaps

 
 
Marilyn Hart
Guest
Posts: n/a
 
      01-22-2005
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


 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      01-22-2005
"Marilyn Hart" <marilyn-paul"NoSpam"@hart-giggins.freeserve.co.uk> wrote in
message news:cstmkq$7tb$(E-Mail Removed)...
[...]
> 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.


 
Reply With Quote
 
 
 
 
Bjorn Abelli
Guest
Posts: n/a
 
      01-22-2005

"Marilyn Hart" wrote...

> 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/...l/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


 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      01-22-2005
"Ryan Stewart" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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());


 
Reply With Quote
 
Marilyn Hart
Guest
Posts: n/a
 
      01-22-2005
----- Original Message -----
From: "Bjorn Abelli" <(E-Mail Removed)>
Newsgroups: comp.lang.java.programmer
Sent: Saturday, January 22, 2005 2:51 PM
Subject: Re: Help on Hashmaps


>
> "Marilyn Hart" wrote...
>
> > 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/...l/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
>
>

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);
}



 
Reply With Quote
 
Bjorn Abelli
Guest
Posts: n/a
 
      01-23-2005

"Marilyn Hart" wrote

> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NewBee help with HashMaps - extra psmith@mcwy.com Java 10 02-28-2007 04:01 AM
NewBee help with HashMaps psmith@mcwy.com Java 1 02-27-2007 03:55 PM
NewBee help with HashMaps psmith@mcwy.com Java 5 02-25-2007 11:15 AM
HashCodes and HashMaps Roedy Green Java 2 09-20-2005 05:07 PM
Maps and Hashmaps neelu111@hotmail.com Java 2 05-25-2004 01:47 PM



Advertisments