Bowling game project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Bowling game project

Hello, I am learning java for 3 days and I deal with that bowling game. I am trying to solve the problem with the following code but I don't understand (for now) the errors. Am I on the right way or should I use the "iterator" ? I get lost between the several possibilities. 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); } public void getWinner(); int maxValueHashMap=(Collections.max(players.values())); // je recherche la valeur maxiamle parmi les valeurs for (Entry<String, Integer> entry : players.entrySet()); { // Iterate through the map if (entry.getValue()==maxValueHashMap) { //if entry == maxValueMap System.out.println(entry.getKey()); // Print the key with max value } } } 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(); } }

20th May 2021, 7:31 AM
morgane bunel
5 Answers
- 1
morgane bunel 3 mistakes: 1 - there is semicolon after method getWinner() 2 - there is semicolon after for loop in getWinner method 3 - There should be Map.Entry in for loop instead of Entry
20th May 2021, 9:14 AM
A͢J
A͢J - avatar
+ 2
Look at the method getWinner(). There is a problem in syntax of definition of getWinner method
20th May 2021, 7:53 AM
Akshay Harshora
Akshay Harshora - avatar
+ 1
Thank you, both of you !
21st May 2021, 1:48 PM
morgane bunel
+ 1
I also missed one pair of {}. Thanks a lot !
21st May 2021, 2:05 PM
morgane bunel
- 4
Greetings! I have been trying to achieve an output of "John" but it is including the score which doesn't need to be shown? Please help me drop the 100 from the output? Thank you for your time and consideration! import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Bowling { public static <K, V extends Comparable<V>> Map.Entry<K, V> getMaxEntryInMapBasedOnValue(Map<K, V> map) { Map.Entry<K, V> entryWithMaxValue = null; for (Map.Entry<K, V> currentEntry : map.entrySet()) { if (entryWithMaxValue == null || currentEntry.getValue().compareTo(entryWithMaxValue.getValue()) > 0) { entryWithMaxValue = currentEntry; } } return entryWithMaxValue; } public static void print(Map<String, Integer> map) { System.out.print("Map: "); if (map.isEmpty()) { System.out.println("[]"); } else { System.out.println(map); } } public static void main(String[] args) { Map<Strin
8th Jul 2021, 2:23 AM
Charles
Charles - avatar