0

I am experimenting with the toString() method on a hash map that I have created for a small application. The map stores three params aName aDate aTime.

However when I run my for-loop it merely prints out the hash code for the map rather than a textual representation which I need for each key value pair.

for (Integer name: activities.keySet()) {
    String key =name.toString();
    String value = activities.get(name).toString();
    System.out.println(key + " " + value);
}

I was wondering where I am going wrong with this function. Any steering would be appreciated. My 2 classes are below.

WhatsOn.java

import java.util.*;

public class WhatsOn {
    public static void main(String[] args) {
        WhatsOn alexa; // create an instance of the WhatsOn class
        alexa = new WhatsOn();
        alexa.addActivity("wash car","010117","0900");
        alexa.addActivity("go shopping","020117","1000");
        alexa.addActivity("return sale items","010117","1000");
        alexa.addActivity("back to work", "040117", "0900");

        for (Integer name: activities.keySet()) {
            String key =name.toString();
            String value = activities.get(name).toString();
            System.out.println(key + " " + value);
        }  

    }

    private String today;
    private int nextId;
    private static Map<Integer, Activity> activities;

    public WhatsOn() {
        activities = new HashMap<Integer, Activity>();
        today = "010117";
        nextId = 1;
        System.out.println("if you see this, the constructor is working");
    }

    // This method should create an instance of Activity class  and then add it to the map referenced by the current value of nextId as the key
    public void addActivity (String aName, String aDate, String aTime) {
        Activity actToAdd = new Activity(aName, aDate, aTime); //create a new instance of the activity called actToAdd, this is a local var that stores methods arguments
        activities.put(nextId, actToAdd); //Add this instance to your Map
        nextId++; //increase the nextId
    }
}

Activity.java

public class Activity {
    private String name;
    private String date;
    private String time;

    Activity(String name, String date, String time) {
        this.name = name;
        this.date = date;
        this.time = time;
    }

    public void setDate(String aDate) {
        this.date = aDate;
    }

    public void setTime(String aTime) {
        this.time = aTime;
    }

    public void setName(String aName) {
        this.name = aName;
    }

    public String getDate() {
        return this.date;
    }

    public String getTime() {
        return this.time;
    }

    public String getName() {
        return this.name;
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
weng tee
  • 372
  • 1
  • 3
  • 16

1 Answers1

1

you need to overwrite the toString method in the Activity class. Something like this:

public String toString(){
  return getName() + getDate() + getTime();
}

I would suggest you use your IDE's generator functionality to generate the toString method. (see also How to override toString() properly in Java?)

side notes

1) Your naming of variables is maybe not the best. Naming an Integer "name" in your for loop is confusing, when a member variable of the Activity is also called "name" and in fact a String.

2) In your loop you access the HashSet again. This is not very elegant, since you are right now cycling over the keySet. Instead look into getting the the whole EntrySet while looping:

for (Map.Entry<Integer,Activity> entry: activities.entrySet()) {
        String key = entry.getKey().toString();
        String value = entry.getValue().toString();
        System.out.println(key + " " + value);
} 
luksch
  • 11,497
  • 6
  • 38
  • 53