0

Find my error in JAVA bowling game??

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 maxValue=(Collections.max(players. values())); for(Map.Entry<String, Integer>entry : players.entrySet()){ if(entry.getValue()==maxValue){ System.out.println(entry.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); 0301 } game.getWinner(); }

1st Oct 2021, 10:43 AM
Md Saidul Bhuiyan
Md Saidul Bhuiyan - avatar
2 Antworten
+ 2
First of all, you putted "0301" in your code for no reason. Also you missed a curly bracket at the end. And finally you should use .equals() rather than == here This code shall work: https://code.sololearn.com/cN404kU39J79/?ref=app
1st Oct 2021, 11:21 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Hi Aleksei, I haven't seen the "Map.Entry" in any of the lessons... or I assume I missed it. Do you happen to know where it is to read about? Also, if one is using the Collections.max, why do you then need the "equals()" at all? I figured (again incorrectly it seems) that you could just output the Max value. Any thoughts there? Thanks in advance!!
3rd Nov 2021, 9:53 PM
Ausgrindtube
Ausgrindtube - avatar