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.

Recommended Answers

All 5 Replies

Did you get the solution

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?

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.