Snowelle
Snowelle

Reputation: 15

how to pass parameters to an array in another class?

This is my first time posting on StackOverflow, but I have visited many times. I am taking a computer science class and I ran into a problem that I have not been able to resolve through research. The assignment is to create a NBATeam class with instance variables and constructors and a NBA client that receives parameters of the names of players from the user and passes them to an array in the first class.

So the problem that I ran into is when I run my program, and I enter player names and pass them to the array, the first array of names are deleted. The names entered into the most recent array are fine, however.

I apologize if my terminology is wrong I am still a newb!

Thank you so much for any help you can give me.

I will insert my code below. My problem is where it says [null]. Also I entered bob for the first team and brian for the second team but bob is in the second team's array.

Please help!

Create NBA team of Heats
Add a player to team Heats? (yes/no)
yes
Enter the name of the player: 
bob
Add one more player to Heats?
no
Create NBA team of Spurs
Add a player to team Spurs? (yes/no)
yes
Enter the name of the player: 
brian
Add another player to Spurs?
no
***Heat Wins!***
Heats[null]Wins: 4 Loses: 0
Spurs[bob]Wins: 0 Loses: 4

import java.util.*;

public class NBA {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String ifAddPlayer, playerName = null;


    //construct Team Heat
    System.out.println("Create NBA team of Heats");
    NBATeam heats = new NBATeam("Heats");
    System.out.println("Add a player to team Heats? (yes/no)");
    ifAddPlayer = input.next();

        while(ifAddPlayer.equalsIgnoreCase("yes")) {
            System.out.println("Enter the name of the player: ");

            heats.addPlayer(playerName);
            playerName = input.next();

            System.out.println("Add one more player to Heats?");
            ifAddPlayer = input.next();
        }

    //construct team Spurs.
    System.out.println("Create NBA team of Spurs");
    NBATeam spurs = new NBATeam("Spurs");
    System.out.println("Add a player to team Spurs? (yes/no)");
    ifAddPlayer = input.next();

        while(ifAddPlayer.equalsIgnoreCase("yes")) {
            System.out.println("Enter the name of the player: ");

            spurs.addPlayer(playerName);
            playerName = input.next();

            System.out.println("Add another player to Spurs?");
            ifAddPlayer = input.next(); 
        }

        playAGame(heats,spurs);
         System.out.println(heats.toString());
            System.out.println(spurs.toString());

}
public static void playAGame(NBATeam heats, NBATeam spurs) {

        for(int game = 0; game < 7; game++){

        double score = Math.random();

        if (score < .5) {
        heats.winAgame(spurs);

        }
        else{    
        spurs.winAgame(heats);

        }
        if(spurs.nWin()==4 || heats.nWin()==4) {
            break;
        }
        }
        if(spurs.nWin()>heats.nWin()) {
            System.out.println("***Spurs Win!***");
        }
        else
            System.out.println("***Heat Wins!***");
    }
}




import java.util.Arrays;

public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private int nPlayer;

private String [] playerArray = new String[nPlayer];

public NBATeam(String sTeamName) {
    this.sTeamName = sTeamName;
    nPlayer=0;

}
public int nWin() {
    return nWin;
}
public int nLoss() {
    return nLoss;
}
public String getTeamName(){        
    //gets team's Name
    return sTeamName;
}

public String toString() {
     return sTeamName + Arrays.toString(playerArray)+ "Wins: " + nWin + 
            " Loses: " + nLoss;

}

public void lossAgame() {
    this.nLoss++;
}
public void winAgame(NBATeam team2) { //To win over the team
    nWin++;
    team2.setLoss(team2.nLoss()+1);
    //teamB.lossAgame();
}

public int getLossNumber() {
    return this.nLoss;
}
public void setLoss(int l) {
    this.nLoss=l;
}

public int getWinNumber() {
    return this.nWin;
}
public void setWin(int w) {
    this.nWin=w;
}
public void addPlayer(String player) {

    String temp[] = playerArray;
    playerArray = Arrays.copyOf(temp, temp.length+1);
    playerArray[nPlayer] = player;
    nPlayer++;

}

public String[] getPlayerArray() {
    return playerArray;
}

public void setPlayerArray(String[] playerArray) {
    this.playerArray = playerArray;
}
}

Upvotes: 0

Views: 385

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201419

You start with an initial value of null in playerName, and then you add the playerName before getting the value. This,

heats.addPlayer(playerName);
playerName = input.next();

is reversed. It should be

playerName = input.next();
heats.addPlayer(playerName);

And, you were consistent. This,

spurs.addPlayer(playerName);
playerName = input.next();

is likewise reversed. And should be

playerName = input.next();
spurs.addPlayer(playerName);

I would suggest you limit the visibility of your variables. If you had

String playerName = input.next();
heats.addPlayer(playerName);

it would limit the possibility of adding the player before you set it.

Upvotes: 1

Related Questions