3 Replies - 5248 Views - Last Post: 25 July 2010 - 06:39 PM Rate Topic: -----

#1 Jacob1805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-July 10

Java Array Addition using user-defined class

Posted 25 July 2010 - 11:44 AM

Hey, I'm creating a small code using arrays and a user-defined class. The goal is to have the user input the amount of players on a baseball team and the amount of games played. For each player and each game the user inputs data that includes the amount of hits, walks, and outs made.

The code I've written can produce output for each player for 1 game, but when I attempt to add information for another game the program is supposed to add the stats together in the output. So when I input for game 1 that player 1 had 2 outs, 2 hits, and 2 walks and for game 2 player 1 had 1 out, 1 hit, and 1 walk it should output 3 outs, hits, and walks. It is only printing out the stats for game 2 though.

I've written classes before that did something very similar with little to no problems. I've searched over my book, the internet, and multiple forums, but I haven't found what I might be doing wrong.

Definitely not looking for a direct answer, but a point in the right direction would be greatly appreciated. I imagine I'm overlooking something fairly obvious.

The class is as follows:

public class Player 
{
	private int hits;
	private int walks;
	private int outs;
	
	public Player()
	{
		hits = 0;
		walks = 0;
		outs = 0;
	}
	
	public int getHits()
	{
		return hits;
	}
	
	public int getWalks()
	{
		return walks;
	}
	
	public int getOuts()
	{
		return outs;
	}
	
	void setHits(int hitsInt)
	{
		hits = hitsInt + hits;
	}
	
	void setWalks(int walksInt)
	{
		walks = walksInt + walks;
	}
	
	void setOuts(int outsInt)
	{
		outs = outsInt + outs;
	}
	
	public String toString()
	{
		String s = new String();
		
		s = hits + "\t" + walks + "\t" + outs;
		return s;
	}


}



The driver program is as follows:

public class Baseball2 
{
	static Scanner console = new Scanner(System.in);
	
	public static void main(String[] args) 
	{
		
		System.out.print("This program tracks a baseball player's number and their " + "\n" +      // Ask User For Team Size
						"number of hits, walks, and outs for each game in a season. " + "\n" +
						"Only nine players bat each game." + "\n" + "\n" +
						"How many players are on your team? ");
		int teamSize = console.nextInt();
		
		System.out.print("How many games are played this season? ");                               // Ask User For Amount of Games
		int gameAmt = console.nextInt();
		
		
		
		
		
		Player[] teamArray = new Player[teamSize];              // Array Declaration
	
		
		for (int j = 0; j < gameAmt; j++)                       // Determines how many times we ask for 9 player's data
		{
			for (int i = 0; i < 9; i++)                         // Asks for 9 players data
			{
				System.out.println();
				System.out.print("For game " + (j+1) + " enter the player number (-1 to quit): ");   //Player number 
				int playerNum = console.nextInt();                                                    //(Not to exceed the boundaries of the array) 
				teamArray[playerNum-1] = new Player();
				System.out.print("Enter the hits for game " + (j+1) + " for player " + playerNum + ":");   // Amount of hits in a game
				int hit = console.nextInt();
				teamArray[playerNum-1].getHits();
				teamArray[playerNum-1].setHits(hit);
				System.out.print("Enter the walks for game " + (j+1) + " for player " + playerNum + ":");  // Amount of walks in a game
				int walk = console.nextInt();
				teamArray[playerNum-1].getWalks();
				teamArray[playerNum-1].setWalks(walk);
				System.out.print("Enter the outs for game " + (j+1) + " for player " + playerNum + ":");    // Amount of outs in a game
				int out = console.nextInt();  
				teamArray[playerNum-1].getOuts();
				teamArray[playerNum-1].setOuts(out);
				
				
			}
		}
	
		System.out.println();
		System.out.print("Player" + "  " + "Runs" + "   " + "Walks" + "   " + "Outs");              // Output to be displayed
		System.out.println("\n" + "------" + "  " + "-----" + "   " + "----" + "   " + "----");        
		for (int t = 0; t < teamArray.length; t++)
			
			
			
			if (teamArray[t] == null)
			{
				System.out.println(" " + (t+1) + "\t" + "0" + "\t" + "0" + "\t" + "0");
			}
			else
			System.out.println(" " + (t+1) + "\t" + teamArray[t]);
			
		

	}

}




Any help you can provide would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Array Addition using user-defined class

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Array Addition using user-defined class

Posted 25 July 2010 - 12:35 PM

It seems you are creating a new Player() each game:

Quote

teamArray[playerNum-1] = new Player(); //inside your for loop



you should create your array of players outside of your for loop:

for(int i = 0; i < teamArray.length; i++){
     teamArray[i] = new Player();
}


This post has been edited by Luckless: 25 July 2010 - 12:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 Jacob1805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-July 10

Re: Java Array Addition using user-defined class

Posted 25 July 2010 - 01:01 PM

View PostLuckless, on 25 July 2010 - 11:35 AM, said:

It seems you are creating a new Player() each game:

Quote

teamArray[playerNum-1] = new Player(); //inside your for loop



you should create your array of players outside of your for loop:

for(int i = 0; i < teamArray.length; i++){
     teamArray[i] = new Player();
}




Thank you! I changed the array creation to make it before the loop and it works just fine now. It runs exactly as it should. Your explanation made it very clear where the problem was, I see what you mean now how the array shouldn't have been inside the loop and that's why there was an issue.
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Array Addition using user-defined class

Posted 25 July 2010 - 06:39 PM

for clarification, it's not so much that the array is in the loop (though that is a problem in your case). The thing is that you were filling the array with NEW Player()s each time a different game was being played. Glad I could help :bigsmile:
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1