I am attempting to the read-in a .txt file and create a map to calculate total property listed in dollars and cents for each agent id. The agent ids are the three digit numbers at the end of each line. The problem is, I’m not exactly sure how to go about doing this.
I’m thinking I should create a while loop and add each line to an arraylist, and then….. I’m lost.
110001 commercial 500000.00 101
110223 residential 100000.00 101
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110
Ok, I know It has been a long time since I started this thread, but I haven’t been able to revisit this problem for weeks now. (I’m attempting to learn this for personal growth, so it hasn’t been priority) So far I have come this far…
My method for creating the map is this:
public void createMap()
{
if(in != null)
{
while(in.hasNextLine())
{
String s = in.nextLine().trim();
System.out.print(s);
String[] p = s.split(" ");
String agentId = p[3];
double property = Double.valueOf(p[2]);
if(!map.containsKey(agentId)){
//if not then add the new key with new value
map.put(agentId, property);
in.nextLine();
}
else
{ //if key is present, add the value..
map.put(agentId, map.get(agentId) + property);
in.nextLine();
}
}
}
}
Then in my program that I am running:
I convert map to a Set and using a enhanced for-loop print each key and value.
Set<String> keySet = examp.map.keySet();
for (String key : keySet)
{
double value = examp.map.get(key);
System.out.println(key + "->" + value);
}
my output however gives me this: {} “empty brackets”!
What am I doing wrong?
If you have it in an arraylist, you could do something like:
You can iterate over the hash map’s keys to get the results.