Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 10:06 AM
Member
 
Join Date: Jan 2008
Posts: 5
otoro_java is on a distinguished road
HashMap with objects
Hi,

Trying to learn HashMap and have lots of questions, so please excuse my ignorance.

I have two classes:
HashMapDemo.class
Account.class

Program is to do simple arithmetic, and write/read from file later on.

So far this is what I have:

Code:
//Account class public class Account{ String customerAccount; String customerName; double customerBalance; public Account(String customerAccount, String customerName,double customerBalance){ //Constructor this.customerAccount = customerAccount; this.customerName = customerName; this.customerBalance = customerBalance; } public String toString() { String output = customerAccount + " " + customerName + " " + customerBalance; return output; } public double doDeposit(double newDeposit){ //Purpose is to get the existing customer balance and add the new amount through input from joptionpane Double output = customerBalance + newDeposit; return output; }
Code:
//HashMapDemo class import java.util.*; public class HashMapDemo { int accountBalance = 100; public static void main(String[] args) { HashMap hm = new HashMap(); Account anAcct1 = new Account("12-12-12", "Joe", 600); Account anAcct2 = new Account("45-45-45", "Jane", 500); //Account methods Account methodAccount = new Account(); hm.put("100", anAcct1); hm.put("200", anAcct2); System.out.println("Account number : " + hm); System.out.println("\nMake a new dposit to account"); //This is where I am stuck System.out.println(hm.get("100")+methodAccount.doDeposit(300)); // Function toString() System.out.println("Account number : " + hm); } }
Thanks in advance
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 12:27 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
println method sees your "trouble code" as Object concatenated with double. You either need to force println to take all as string by,
Code:
System.out.println(""+hm.get("100")+methodAccount.doDeposit(300));
Or use Object.toString() method by
Code:
System.out.println(hm.get("100").toString()+methodAccount.doDeposit(300));
One more thing .. you do have default constructor for Account class ..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 05:28 PM
Member
 
Join Date: Jan 2008
Posts: 20
JAdmin is on a distinguished road
As roots pointed out, HashMap stores everything as objects. So when you retrieve an object from HashMap, you need to cast it to the actual type.
__________________
Sincerely, Your friends at
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Soft HashMap Java Tip java.lang 0 04-12-2008 10:45 PM
HashMap and ComboBox banie AWT / Swing 2 03-26-2008 01:58 AM
Hashmap dirtycash New To Java 5 12-03-2007 04:58 AM
what is hashmap gabriel New To Java 5 08-03-2007 03:23 PM
Help with HashMap in java toby New To Java 1 07-25-2007 10:04 PM


All times are GMT +3. The time now is 01:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org