I am moving through some of the revision activities in my college text book, and have gotten stuck on this question. We are working on maps and lists, the question being asked needs me to check if a list for the division already exists in the teams map, and id so add the new team to the division that already exists.
If it doesn't then to create a new empty list and add the new team to it, assigning the division as the key and the new list as the value.
having re-read the material several times, I started to understand what i needed to do, but when I come to compile it gives me a method put interface.java.util.Map error.
Which on explanation offers
The operator that you use here cannot be used for the type of value that you are using it for. You are either using the wrong here, or the wrong operator.
I have re-read the material, researched on line and even bought a udemy course to understand maps and how they work.
Looking at a past exam paper helped and as I understand it the problem lies with the .put action on line 33, but everything I have read is when working with maps the .put is how you add information to the map.
So I don't know why I am getting this error. In desperation, looked at the past exam answer which is the same as mine, and tested their code which compiled with no errors.
I desperately want to understand why as opposed to looking in the back of the material and just looking at the answer as this is for revision.
The code of the LeagueAdmin() class we have to amend is as follows
import java.util.*;
public class LeagueAdmin
{
private SortedMap<String, Set<String>> teams;
/**
* Constructor for objects of class LeagueAdmin
*/
public LeagueAdmin()
{
// Create the HashMap
//Map<String, Team> teams = new HashMap<>();
super();
this.teams = new TreeMap<>();
}
public void addTeam(String division, Team team)
{
boolean changed;
if (!this.teams.containsKey(division)) // checks if the key division doesn't contain the value of divsioin
{
HashSet<Team> teamSet = new HashSet<>(); // instantiates a list of objects called Team and assigns them to local variable teamSet
teamSet.add(team); // adds a new team to the list
this.teams.put(division, teamSet);
changed = true;
}
else
{
Set<Team> teamSet = this.teams.get(division); // if list exists already adds new team to existing list
changed = teamSet.add(team);
}
}
}
we were also given another class teams which I have included below.
/**
* class Team for TMA03Q2.
*
* @author (M250 module team)
* @version (1.0)
*/
public class Team
{
private String name;
private String division;
private int won;
private int drew;
private int lost;
// no need to record points as = 3*won + drew
/**
* Constructor for objects of class Team
*/
public Team(String aName, String aDivision)
{
name = aName;
division = aDivision;
// no need to set won, drew and lost to 0
}
/**
* getter for attribute points
*/
public int getPoints()
{
return 3 * won + drew;
}
/**
* getter for name
*/
public String getName()
{
return name;
}
/**
* getter for division
*/
public String getDivision()
{
return division;
}
/**
* getter for won
*/
public int getWon()
{
return won;
}
/**
* getter for drew
*/
public int getDrew()
{
return drew;
}
/**
* getter for lost
*/
public int getLost()
{
return lost;
}
/**
* increments the number of games won
*/
public void incWon()
{
won = won + 1;
}
/**
* increments the number of games drawn
*/
public void incDrew()
{
drew = drew + 1;
}
/**
* increments the number of games lost
*/
public void incLost()
{
lost = lost + 1;
}
/**
* setter for division
*/
public void setDivision(String aDivision)
{
division = aDivision;
}
public String toString()
{
return ("Team " + name + ", division: " + division + " stats: Won: " + won
+ ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());
}
}
it should accept the new division and team, check if there is a list already and update accordingly but I can't get past this error.
So any help would be greatly received on where I have gone wrong. As I want to understand how the problem has occurred and not just look at an answer and not understand.