+ 1

Pls rectify the errors.

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 int getWinner() { Iterator<String> it = players.iterator(); if(players.hasNext()) { int max = it.next(); if(max < it.next()) max = it.next(); } } } 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(); } }

30th Nov 2022, 7:24 AM
Devishree
Devishree - avatar
8 Answers
+ 2
For hashMap, entries will be <String, integer> pair so single <String> type can't work. You need to use entrySet like : Iterator< Entry<String,Integer> > it = players.entrySet().iterator(); // this iterator ( it.next() ) returns a pair set (key, value) , not single element. Otherwise for single Key iteration, Use keySet of Set like : Set<String> l = players.keySet(); Iterator<String> it = l.iterator(); This iterator (it.next() ) returns a single String of elements from the KeySet each iteration. Note: you are using it.next() 3 times in if block so each time, it will return different next value. Not the same value so only call one time in iteration and store in variable to use further. Your iterator it.next() returns a string storing in int type by int max = it.next() ; is also a error...
30th Nov 2022, 8:21 AM
Jayakrishna 🇮🇳
Jayakrishna 🇮🇳 - avatar
+ 2
Can you pls suggest me a proper code for this. This is the whole question. The given code declares a Bowling class with its constructor and addPlayer() method. Each player of the game has a name and points, and are stored in the players HashMap. The code in main takes 3 players data as input and adds them to the game. You need to add a getWinner() method to the class, which calculates and outputs the name of the player with the maximum points. Sample Input: Dave 42 Amy 103 Rob 64 Sample Output: Amy
30th Nov 2022, 9:07 AM
Devishree
Devishree - avatar
+ 2
Thank you
30th Nov 2022, 10:32 AM
Devishree
Devishree - avatar
+ 1
Ok
30th Nov 2022, 8:38 AM
Devishree
Devishree - avatar
+ 1
Sorry,again it's showing errors: 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 int getWinner() { Iterator<Entry<String, Integer>> it = players.entryset().iterator(); if(players.hasNext()) { int max = it.next(); if(max < it.next()) max = it.next(); } } } 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(
30th Nov 2022, 9:05 AM
Devishree
Devishree - avatar
+ 1
Jayakrishna🇮🇳 Almost the full code was given already. Only We have to add a getWinner() method and calculate max points. I am not able to understand where is the mistake 🤔
30th Nov 2022, 9:10 AM
Devishree
Devishree - avatar
+ 1
This was the code which was already given 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 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(); } }
30th Nov 2022, 9:26 AM
Devishree
Devishree - avatar
+ 1
As I already mentioned : Iterator<Entry<String,Integer>> it = players.entrySet().iterator(); it.next() returns pairSet( key, value) form. Not single String key. So you need to store in : Map.Entry<String, Integer> e = ( Map.Entry<String, Integer> ) it.next(); Next e.getKey() returns key e.getValue() returns value. player.hasNext() is invalid. Use iterator like while( it.hasNext() ) { // Map.Entry..... add here if( max < e.getValue() ) { max = e.getValue (); name = e.getKey(); } } Finally display name. Note: Declare all variable int max=0; before loop. and String name; Import java.util.Map.Entry; edit: Devishree if found difficult, try to use otherways from this link : https://www.geeksforgeeks.org/how-to-iterate-hashmap-in-java/amp/
30th Nov 2022, 10:12 AM
Jayakrishna 🇮🇳
Jayakrishna 🇮🇳 - avatar