the following program first takes two integers,number of legal football players(n) and the number of teams(m). at next n lines it takes the names of the legal players and puts them as the key of a hashmap, with all their values string zero. then it takes the name of the teams and the players they have chosen. if a team chooses a player who's name is not among the legal players, the team has broken the law and his name will be printed as guilty, or if a team chooses a player which is already taken by another team, both of the teams names will be printed. i put then in a treeset cause i want the names to be printed in alphabetic order. but when i run the program, all the teams names are printed where is the problem?
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
public class Tamrin1_2_1 {
public static void main(String[] args) {
String player,team;
int numberOfTeams,numberOfPlayers;
Scanner scanner=new Scanner(System.in);
HashMap<String,String> teamsAndPlayers=new HashMap<String,String>();
TreeSet<String> guilty=new TreeSet<String>();
numberOfPlayers=scanner.nextInt();
numberOfTeams=scanner.nextInt();
for(int i=0;i<=numberOfPlayers;i++) {
teamsAndPlayers.put(scanner.nextLine(), "0");
}
for(int i=0;i<numberOfTeams;i++) {
team=scanner.nextLine();
numberOfPlayers=scanner.nextInt();
for(int j=0;j<=numberOfPlayers;j++) {
player=scanner.nextLine();
if(teamsAndPlayers.containsKey(player)) {
if(teamsAndPlayers.get(player).equals("0")) {
teamsAndPlayers.put(player, team);
}
else {
guilty.add(team);
guilty.add(teamsAndPlayers.get(player));
}
}
else {
guilty.add(team);
}
}
}
System.out.println(guilty);
}
}
this is a sample input:
10 4
dani carvajal
eder militao
sergio ramos
raphael varane
nacho
eden hazard
toni kroos
martin odegaard
karim benzema
luka modric
real madrid
3
martin odegaard
karim benzema
sergio ramos
valencia
2
dani carvajal
luka modric
real sociedad
2
martin odegaard
nacho
atletico madrid
3
raphael varane
eden hazard
toni kroos
my desired output is:
real madrid
real sociedad
but the output i get is:
atletico madrid
real madrid
real sociedad
valencia
Actually I would suspect these for-loop conditions:
for(int i=0;i<=numberOfPlayers;i++) {
and
for(int j=0;j<=numberOfPlayers;j++) {
Use <
instead of <=
. I suspect that now you are reading one more name into each team than required.
Also, after you read an integer value the cursor is still on the same line, just in front of the "\n", so the next scanner.nextLine() call does not return the string of the next line, but an empty string. Because of that you got an additional player with empty name, which you also tried to put into every team, so every team was marked as guilty. You can read more about it here. To solve this you can, for example, place an additional scanner.nextLine() after you have read an integer and wants to read a string next:
public class Tamrin1_2_1 {
public static void main(String[] args) {
String player,team;
int numberOfTeams,numberOfPlayers;
Scanner scanner=new Scanner(System.in);
HashMap<String,String> teamsAndPlayers=new HashMap<String,String>();
TreeSet<String> guilty=new TreeSet<String>();
numberOfPlayers=scanner.nextInt();
numberOfTeams=scanner.nextInt();
scanner.nextLine();
for(int i=0;i<numberOfPlayers;i++) {
teamsAndPlayers.put(scanner.nextLine(), "0");
}
for(int i=0;i<numberOfTeams;i++) {
team=scanner.nextLine();
numberOfPlayers=scanner.nextInt();
scanner.nextLine();
for(int j=0;j<numberOfPlayers;j++) {
player=scanner.nextLine();
if(teamsAndPlayers.containsKey(player)) {
if(teamsAndPlayers.get(player).equals("0")) {
teamsAndPlayers.put(player, team);
}
else {
guilty.add(team);
guilty.add(teamsAndPlayers.get(player));
}
}
else {
guilty.add(team);
}
}
}
System.out.println(guilty);
}
}