Results 1 to 3 of 3
Thread: HashMap with objects
- 01-28-2008, 08:06 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 5
- Rep Power
- 0
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:
Java 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; }Thanks in advanceJava 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"); [COLOR="Red"]//This is where I am stuck[/COLOR] System.out.println(hm.get("100")+methodAccount.doDeposit(300)); // Function toString() System.out.println("Account number : " + hm); } }
- 01-28-2008, 10:27 AM #2
println method sees your "trouble code" as Object concatenated with double. You either need to force println to take all as string by,
Or use Object.toString() method byJava Code:System.out.println(""+hm.get("100")+methodAccount.doDeposit(300));
One more thing .. you do have default constructor for Account class ..Java Code:System.out.println(hm.get("100").toString()+methodAccount.doDeposit(300));dont worry newbie, we got you covered.
- 01-28-2008, 03:28 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 20
- Rep Power
- 0
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 www.javaadvice.com
Similar Threads
-
Soft HashMap
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:45 PM -
HashMap and ComboBox
By banie in forum AWT / SwingReplies: 2Last Post: 03-25-2008, 11:58 PM -
Hashmap
By dirtycash in forum New To JavaReplies: 5Last Post: 12-03-2007, 02:58 AM -
what is hashmap
By gabriel in forum New To JavaReplies: 5Last Post: 08-03-2007, 01:23 PM -
Help with HashMap in java
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks