I have an ArrayList that contains Objects. What I have been wanting/needing to do is search for a specific Object within the ArrayList and alter one of it’s values if found. I.E. I have a team of Football Players, say Philadelphia Eagles that contains the team name, total points scored total sacks, and then lists all the players by first name, last name, games played, and position. I am looking for a specific player say Chad Hall and I want to remove him from the Eagles and trade him to the New England Patriots. What I have so far is the iteration of the ArrayList but my code isn’t finding the player and then exits out.
case 6:
String name = "";
System.out.print("Who would you like to trade? ");
name = kb.nextLine();
tradePlayer(nflTeams, name);
public static void tradePlayer(ArrayList<Team> nflTeams, String name)
{
Scanner kb = new Scanner(System.in);
String teamName = "";
System.out.print("What team do you want to trade this player to?(Team Acronym) " );
teamName = kb.nextLine();
for(Team t: nflTeams){
if (nflTeams.contains(name.toLowerCase()))
t.setName(teamName);
}
System.out.println("Cannot find " + name);
}
The Player class involved constructed players by firstname, lastname, team, position, played. What I want to alter is the team. In the Team class I have the setName(String) to set team names.
Any suggestions or thoughts are much appreciated.
if (nflTeams.contains(name.toLowerCase()))What are you trying to do? Your list
nflTeamscontainsTeamobjects not names.This can not work.
If you need to access a
Teamvia itsnamethen useMap<String,Team>instead