Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Zesheng Li
3,013 PointsSoccer League Organizer Stuck on adding player to a team.
public void addPlayerToTeam() throws IOException{ List<String> firstNameList = new ArrayList<>(); List<String> lastNameList = new ArrayList<>(); String firstName; String lastName; int height; boolean exp;
System.out.println("Players choose a team");
team = mReader.readLine();
System.out.println("Here is the free agent you can choose: ");
freeAgent();
do{
System.out.println("Please Enter the First Name of the Player: ");
firstName = mReader.readLine().toLowerCase();
if(firstNameList.contains(firstName)==true){
System.out.println("Can not fine first name. ");
}
}while(firstNameList.contains(firstName)==true);
do{
System.out.println("Please enter the Last Name of the Player: ");
lastName = mReader.readLine().toLowerCase();
if(firstNameList.contains(lastName)==true){
System.out.println("Can not fine last name. ");
}
}while(firstNameList.contains(lastName)==true);
List<Player> temp = new ArrayList();
temp.add(firstName,lastName,height,exp);
teamAndPlayer.put(team,temp);
Here is the code so far. I am stuck on how to get the rest of the information of the player that is selected. Some one please help.
3 Answers

Timothy Hilley
2,292 Pointshonestly not to bash, but i would have used a map instead of an array list. but:
private List<Players> team;
Player[] players = Players.load();
public void pickAPlayer(String firstorlastName) {
for ( Player player : players ) {
String fname = player.getFirstName();
String lname = player.getLastName();
String fullName = lname + ", " fname;
if(fullname.contains(firstorlastName) {
team.add(player);
}
}
}

Timothy Hilley
2,292 Pointscan you post all pages of your code please

Zesheng Li
3,013 Pointshttps://github.com/lzcscofield/TreeHouseSoccerLeague here, you can see all my file. So what I want is to add the player to team if one player is chosen by user. the players is a string[] type. how can I add all the data into a list.
Thank you so much.

Timothy Hilley
2,292 Pointschange players to a
import java.util.Map;
import java.util.TreeMap;
import java.util.List;
Map<String fullName, Player players> playersForPicking = new TreeMap<>;
so that you can call a player and its an object of Player, so you can use the gets. i updated. this sounds like you need a map of players, each player object will be represented by the combenation of
String fullName = lastName + ", " + firstName;
call it by
Player newPlayer = playersForPicking.get(fullName); //returns the Player object instance of that player
List<Player> team;
team.add(newPlayer)

Zesheng Li
3,013 Pointshi, so for player it have different type string, string,int and boolean. for example there are a player ("Joe", "Smith", 42, true) how do I search firstName and lastName and than it will return me whole data?
Zesheng Li
3,013 PointsZesheng Li
3,013 PointsI see what you mean. this helps a lot than you so much.
Timothy Hilley
2,292 PointsTimothy Hilley
2,292 Pointshappy to help and jog my brain for practice :D