java - Get specific Data from inside a HashMap -


first of must apologies uncertain how word title well.

however problem facing continuation of question brought me 1 step closer completing particular program. onto problem though.

here current output:

income {jack=46, mike=52, joe=191} 

these inside hashmap , printed out, need though make output more presentable , guess leads needing manipulate/get data inside map , make presentable.

the goal aiming output this:

jack: $191 mike: $52 joe: $46 

i'm still new java , programming in general i'm wondering if possible or if have tackled wrong way in beginning?

below code:

public static void main(string[] args) {    string name;   int leftnum, rightnum;    //scan text file   scanner scan = new scanner(test3.class.getresourceasstream("pay.txt"));    map < string, long > namesummap = new hashmap < > (3);   while (scan.hasnext()) { //finds next line     name = scan.next(); //find name on line     leftnum = scan.nextint(); //get price     rightnum = scan.nextint(); //get quantity      long sum = namesummap.get(name);     if (sum == null) { // first time see "name"       namesummap.put(name, long.valueof(leftnum + rightnum));     } else {       namesummap.put(name, sum + leftnum + rightnum);     }   }   system.out.println("income");   system.out.println(namesummap); //print out names , total next them    //output looks ---> {jack=46, mike=52, joe=191}    //the next problem how names on seperate lines   //and total next names have symbol $ next them.   //also possible change = :   //i need output below   /*       jack: $191       mike: $52       joe: $46   */ }  } 

well instead of relying on default tostring() implementation of hashmap, loop on entries:

for (map.entry<string, long> entry : namesummap.entryset()) {     system.out.println(entry.getkey() + ": $" + entry.getvalue()); } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -