+ 1
Am confused about hashmap in Java
Am trying to get output of this hasmap question: I have players .put("Dave",64); Players.put("Amy",103): Players.put("Rob",74); Now am ask to output Amy the highest score like this Amy with no score just Amy, When I try changing the key to value it says error k,v cannot change to string Please help this question have taken me not less than 8 hours searching the web for help.
20 odpowiedzi
+ 5
Fineb Alex check this out it's perfectly working:
import java.util.HashMap;
import java.util.Map.Entry;
public class MyClass {
public static void main(String[ ] args) {
HashMap<Integer, String> players = new HashMap<Integer, String>();
players.put(74,"Rob");
players.put(154, "Amy");
players.put(103,"Dave");
//keys
for(Entry<Integer,String> entry: players.entrySet()){
System.out.println(entry.getKey());
}
//values
for(Entry<Integer,String> entry: players.entrySet()){
System.out.println(entry.getValue());
}
}
}
+ 5
Fineb Alex you need to find max/highest score (key) from the hash map.
And print the value associated with that key like (154 is key of Amy)
import java.util.HashMap;
import java.util.Map.Entry;
public class MyClass {
public static void main(String[ ] args) {
HashMap<Integer, String> players = new HashMap<Integer, String>();
players.put(74,"Rob");
players.put(154, "Amy");
players.put(103,"Dave");
//Getting max key
int max = Collection.max(players.getKey());
for(Entry<Integer,String> entry: players.entrySet()){
if(max==entry.getKey()){
System.out.println(entry.getValue());
break;
}
}
}
}
Or
System.out.println(players.getValue(max));
Edit: your key and value pair is placed opposite,according to the Challenge String type in the hashmap is key and Integer type is value.
+ 3
A colon after Players.put("Amy",103)
Better a semicolon
+ 2
Try this:
for(Entry<String,Integer> entry: players.entrySet())
System.out.println(entry.getKey());
+ 2
Fineb Alex import this
import java.util.Map.Entry;
+ 1
Please share code
+ 1
I Hv been battling with it since ystd
0
Yes I kn thanks
0
I can't share code is solo question
0
I even write my own code by changing the key to value it works but I can't do same in there code
0
import java.util.*;
public class Bowling {
HashMap<String, Integer> players;
Bowling() {
players = new HashMap<String, Integer>();
}
public void addPlayer(String name, int p) {
players.put(name, p);
}
//your code goes here
public void getWinner(){
players .put("Dave", 42);
players .put("Amy", 103);
players .put ("Rob", 64);
for (String i:players.keySet())
System.out.println(i);
}
}
public class Program {
public static void main(String[ ] args) {
Bowling game = new Bowling();
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++) {
String input = sc.nextLine();
String[] values = input.split(" ");
String name = values[0];
int points = Integer.parseInt(values[1]);
game.addPlayer(name, points);
}
game.getWinner();
}
}
0
Frogged I HV share please help
0
I wrote my by changing the key it
https://code.sololearn.com/cWO0cv1Sk0Pk/?ref=app
0
Error cannot find symbol.
0
Same Result output with this code:
for (String I: players.keySet())
System.out.println(i);
0
The issue here is they want when you can this method game.getwinner();
only only highest score name must display check my code I past above to see how it suppose to be
0
What they want is wen u call 103 = Amy
0
hi
0
Hi Hector 01
0
<\>