Results 1 to 3 of 3
  1. #1
    otoro_java is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default 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;
    	}
    Java 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);
    
      }
    }
    Thanks in advance

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    println method sees your "trouble code" as Object concatenated with double. You either need to force println to take all as string by,
    Java Code:
    System.out.println(""+hm.get("100")+methodAccount.doDeposit(300));
    Or use Object.toString() method by
    Java 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.

  3. #3
    JAdmin is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    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

  1. Soft HashMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:45 PM
  2. HashMap and ComboBox
    By banie in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2008, 11:58 PM
  3. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02:58 AM
  4. what is hashmap
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-03-2007, 01:23 PM
  5. Help with HashMap in java
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •