Member Avatar for Member #413109

Hello, I need help on the NFLTeam program that uses an array on the number of players and their names. I've been doing an NFLTeam program where it prints the wins and losses of both falcons and steelers as well as printing out the number of players in those teams including their names. This is the program that I have so far:

NFLTeam5

public class NFLTeam5
{
	private String[] sPlayerArray;
	private int nTotalNumPlayers;
	private int win;
	private int loss;
	private String TeamName;

	public NFLTeam5(String eName)
	{
		win=0;
		loss=0;
		TeamName = eName;
		nTotalNumPlayers=0;
	}

	public void winAgame()
	{
		win++;
	}

	public void winAgame(NFLTeam5 teamB)
	{
		win++;
		teamB.lossAgame();
	}

	public void lossAgame()
	{
		loss++;
	}

	public void lossAgame(NFLTeam5 teamB)
	{
		loss++;
		teamB.winAgame();
	}

	public int getWinNum()
	{
		return win;
	}

	public int getLossNum()
	{
		return loss;
	}

	public String getName()
	{
		return TeamName;
	}

	public String[] getPlayerName()
	{
		return sPlayerArray;
	}

	public int getTotalPlayersNum ()
	{
		return nTotalNumPlayers;
	}

	public void addAPlayer (String playerA)
	{
	//	nTotalNumPlayers++;
		String [] sPlayerArrayTemp=new String [nTotalNumPlayers];
		for (int i =0,j=0; i< nTotalNumPlayers; i++,j++)
		{
			if (sPlayerArray[j]!=playerA)
				sPlayerArrayTemp[i]=sPlayerArray[j];
			else
				sPlayerArrayTemp[i]=sPlayerArray[++j];
		}
				sPlayerArray=sPlayerArrayTemp;
	}

	public void deleteAPlayer (String playerA)
	{
		nTotalNumPlayers--;
		String [] sPlayerArrayTemp=new String [nTotalNumPlayers];
		for (int i =0,j=0; i< nTotalNumPlayers; i++,j++)
		{
			if (sPlayerArray[j]!=playerA)
				sPlayerArrayTemp[i]=sPlayerArray[j];
			else
				sPlayerArrayTemp[i]=sPlayerArray[++j];
		}
		sPlayerArray=sPlayerArrayTemp;
	}

	public String toString()
	{
		String sOutput=TeamName + " Win/Loss: " + win + " / "+ loss + " games ";
		sOutput = sOutput+ "\n" + "Number of Players is: " + nTotalNumPlayers + "\n";
		//sOutput = sOutput + "\n" + getPlayerName;
		for (int i =0; i< nTotalNumPlayers; i++)
			sOutput = sOutput + sPlayerArray[i] + "\n";
			return sOutput;
	}
}//end of class definition

NFLTeamGameDay5

import jpb.*;

public class NFLGameDay5
{
	public static void main (String [] args)
	{
		//Construct Team Falcons
		NFLTeam5 falcons = new NFLTeam5("Falcons");

		SimpleIO.prompt("How many players Falcons own: ");
		String userInput = SimpleIO.readLine().trim();
		int numberOfPlayers = Integer.parseInt(userInput);

		// Prompt user to enter players into the Team
		for (int i = 0; i < numberOfPlayers; i++)
		{
			SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
			userInput = SimpleIO.readLine().trim();
			falcons.addAPlayer(userInput);
			//falcons.deleteAPlayer(userInput);
		}
		//Construct Team Steelers
		NFLTeam5 steelers = new NFLTeam5("Steelers");
		SimpleIO.prompt("How many players Steelers own: ");
		userInput = SimpleIO.readLine().trim();
		int numberOfPlayers2 = Integer.parseInt(userInput);

		// Prompt user to enter players into the Team
		for (int i = 0; i < numberOfPlayers2; i++)
		{
			SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
			userInput = SimpleIO.readLine().trim();
			steelers.addAPlayer(userInput);
		}

		//Simulate a game
		falcons.lossAgame(steelers);
		System.out.println (falcons);
		System.out.println (steelers);

	}//end of Main method
}//end of class definition

So far, I've got the output for the wins and losses, but it's not printing out the players names and the number of players is always at 0. Am I forgetting something in there? Please reply back. Thanks.

Dani AI

Generated

Good that tracked down the missing initialization — that fixed the symptom (nothing printed) — but the hand-rolled array logic in the later posts still contains fragile patterns that will bite later. The two practical things to keep in mind: never compare Java strings with ==/!= (use equals), and don’t mutate the “count” before you confirm an element was found. The current deleteAPlayer loop with ++j can skip entries and easily cause IndexOutOfBounds, and decrementing nTotalNumPlayers before you actually remove a name will desynchronize your state if the name isn’t present.

If you want minimal fixes while staying with arrays: first search for the index of the player (using equals), only decrement the count when found, then use System.arraycopy or two for loops to copy elements around the removed index. But a cleaner, far safer approach is to use a standard collection designed for dynamic size.

Example using ArrayList (keeps logic simple and avoids manual resizing):

private final List<String> players = new ArrayList<>();

public void addPlayer(String name) {
    if (name == null) return;
    players.add(name.trim());
}

public boolean removePlayer(String name) {
    return players.remove(name); // uses equals internally
}

public int getTotalPlayersNum() { return players.size(); }

public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(TeamName).append(" Win/Loss: ").append(win).append(" / ").append(loss).append("\n");
    sb.append("Number of Players is: ").append(players.size()).append("\n");
    for (String p : players) sb.append(p).append("\n");
    return sb.toString();
}

Final notes: initialize fields clearly in the constructor (set counts, then create the list/array), avoid keeping redundant state (don’t track a separate player count if you can call players.size()), and add simple tests for adding/removing names (including duplicates and absent names). This also addresses the follow-ups from and and will make the class much easier to maintain.

Recommended Answers

All 5 Replies

Did you get the solution

Member Avatar for Member #413109

Did you get the solution

No, not yet. Right now, the only thing that I've done was that I got it to show the number of players where I did

int [] fPlayers = new int[numberOfPlayers];

and did it's output which is

System.out.println("Number of players is: " + fPlayers.length);

I did the same for the steelers as well. The only problem that I have would be printing out the player's names from inputting the number of players. I still don't get how to do that.

Did you figure it out?

How did you end up solving it?

Member Avatar for Member #413109

How did you end up solving it?

Yeah I know how to solve it now. Apparently, I forgot to add the sPlayerArray in the constructor. It turns out I got everything right before I posted the code here. Here's what I got:

NFLGameDay5

import jpb.*;

public class NFLGameDay5
{
	public static void main (String [] args)
	{
		//Construct Team Falcons
		NFLTeam5 falcons = new NFLTeam5("Falcons");

		SimpleIO.prompt("How many players Falcons own: ");
		String userInput = SimpleIO.readLine().trim();
		int numberOfPlayers = Integer.parseInt(userInput);

		// Prompt user to enter players into the Team
		for (int i = 0; i < numberOfPlayers; i++)
		{
			SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
			userInput = SimpleIO.readLine().trim();
			falcons.addAPlayer(userInput);
		}
		//Construct Team Steelers
		NFLTeam5 steelers = new NFLTeam5("Steelers");
		SimpleIO.prompt("How many players Steelers own: ");
		userInput = SimpleIO.readLine().trim();
		numberOfPlayers = Integer.parseInt(userInput);

		// Prompt user to enter players into the Team
		for (int i = 0; i < numberOfPlayers; i++)
		{
			SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
		    userInput = SimpleIO.readLine().trim();
			steelers.addAPlayer(userInput);
		}

		//Simulate a game
		falcons.lossAgame(steelers);
		System.out.println (falcons);
		System.out.println (steelers);



	}//end of Main method
}//end of class definition

NFLTeam5

public class NFLTeam5
{
	private String[] sPlayerArray;
	private int nTotalNumPlayers;
	private int win;
	private int loss;
	private String TeamName;

	// Constructor
	public NFLTeam5(String eName)
	{
		win=0;
		loss=0;
		sPlayerArray = new String [nTotalNumPlayers];
		TeamName = eName;
		nTotalNumPlayers=0;
	}

	// Adds win points
	public void winAgame()
	{
		win++;
	}

	// Shows which player gets win points
	public void winAgame(NFLTeam5 teamB)
	{
		win++;
		teamB.lossAgame();
	}

	// Adds loss points
	public void lossAgame()
	{
		loss++;
	}

	// Shows which player gets loss points
	public void lossAgame(NFLTeam5 teamB)
	{
		loss++;
		teamB.winAgame();
	}

	// returns value of win
	public int getWinNum()
	{
		return win;
	}

	// returns value of loss
	public int getLossNum()
	{
		return loss;
	}

	// returns the team's name
	public String getName()
	{
		return TeamName;
	}

	public String[] getPlayerName()
	{
		return sPlayerArray;
	}

	// returns the number of players
	public int getTotalPlayersNum ()
	{
		return nTotalNumPlayers;
	}
	//adds the player
	public void addAPlayer (String playerA)
	{
		nTotalNumPlayers++;
		String [] sPlayerArrayTemp = new String [nTotalNumPlayers];
		int i;
		for (i=0; i< sPlayerArray.length; i++)
		{
			sPlayerArrayTemp[i]=sPlayerArray[i];//copy old players
		}
		sPlayerArrayTemp[i]= playerA; //adds last new player
		sPlayerArray=sPlayerArrayTemp; //copies into variable
	}

	// deletes the player
	public void deleteAPlayer (String playerA)
	{
		nTotalNumPlayers--;
		String [] sPlayerArrayTemp=new String [nTotalNumPlayers];
		for (int i =0,j=0; i< nTotalNumPlayers; i++,j++)
		{
			if (sPlayerArray[j]!=playerA)
				sPlayerArrayTemp[i]=sPlayerArray[j];
			else
				sPlayerArrayTemp[i]=sPlayerArray[++j];
		}
		sPlayerArray=sPlayerArrayTemp;
	}

	// gives the output
	public String toString()
	{
		String sOutput=TeamName + " Win/Loss: " + win + " / "+ loss + " games ";
		sOutput = sOutput+ "\n" + "Number of Players is: " + nTotalNumPlayers + "\n";
		for (int i =0; i< nTotalNumPlayers; i++)
			sOutput = sOutput + sPlayerArray[i] + "\n";
			return sOutput;
	}
}//end of class definition

So now it pretty much works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.