+ 1

In my getWinner function what is the problem. I am try to loop the hashmap so as to print the key with the highest value.

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(){ int highVal = Collections.max(players.values()); for(Entry<String,Integer>score:players.entrySet()){ if(score.getValue()==highVal){ System.out.println(score.getKey()); } } } } 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(); } }

19th Jul 2022, 10:17 PM
socka
socka - avatar
3 Answers
+ 3
Specify where Entry was defined (HashMap.Entry) for( HashMap.Entry<String, Integer>score : players.entrySet() )
19th Jul 2022, 11:29 PM
Ipang
+ 1
Correct. Thank you
20th Jul 2022, 5:22 AM
socka
socka - avatar
0
If you want to learn with java 8 stream API 😉: https://code.sololearn.com/cl0AJF6gJH4t/?ref=app
20th Jul 2022, 6:12 AM
Roland
Roland - avatar