0

So I have my constructor in this class called Player where each player is an array of 5 (to store their bets in another class):

public class Player {

private int[] anyplayer = new int[5];

// constructor for each player at the table

public Player(int[] anyplayer) {

    this.anyplayer = anyplayer;
}

Now I've unknowingly coded my other class to this:

 int[] player2 = new int[5]; // this is a variable I'm trying to change to the object
        try{

           for(int i=0;i<5;i++) {

               player2[i] = Integer.parseInt(keyin.nextLine());

               }
           }

       catch(NumberFormatException e){  

           System.out.println("Player 3 : ");  
       }

Now I can't seem to find a workaround this without breaking everything! I'm still a newbie at programming, but is there a simple way I could replace the int[] player2 = new int[5]; with an actual object from my constructor like Player player2 = new player(); ?

I've tried that and it keeps saying The constructor Player() is undefined

3
  • Use a java collection instead of array i.e. List or Set for the purpose of storing the int value i.e. the bet. Commented Feb 12, 2015 at 7:17
  • It looks like you're confusing what your Player class is supposed to be. Is it supposed to represent a player? Because at the moment, it's a container for an array of player IDs. Commented Feb 12, 2015 at 11:12
  • constructor Player() is undefined means exactly that. You haven't created a constructor method in the Player class, which doesn't take an argument. Commented Feb 12, 2015 at 11:13

4 Answers 4

3

Your constructor accepts an array of integers, so it will be

int[] player2 = new int[5];
Player somePlayer = new Player(player2);
Sign up to request clarification or add additional context in comments.

Comments

1

Why not create the object using the array you made? Add to the end:

Player player2object = new Player(player2);

4 Comments

When I try to System.out.println(somePlayer[0]); it keeps saying The type of the expression must be an array type but it resolved to Player ? I'm trying to access the first index in the array player2 through my object
You have to add an accessor to your player class so that you can access its instance variable (anyplayer). The somePlayer object contains the array anyplayer. It alone is not an array; it contains one. Add the following method to Player so you can access its array through a method: public int[] getAnyPlayer() { return anyplayer; } You can now do somePlayer.getAnyPlayer()[0]. Note that this code is somewhat dangerous in that it allows the private array to be modified, making the Player class mutable.
you mean a privacy leak? How would I make it not leak
Not a privacy leak persay, but you expose the Player class's anyplayer array to other classes. For example, if you did somePlayer.getAnyPlayer()[0] = 10, you would change the value of the first index in that Player's array. In many cases, this can be considered bad practice. If you want to ensure that values cannot be modified, return anyplayer.clone() instead of anyplayer. This complexity may not be necessary for your application, though.
1

Every Java Class has default empty constructor until you set any other constructor to this object. You have to add new constructor

public Player() {
   //youre code
}

or set number of players in Player constructor call (from @Dmitrii Kondratev answer)

int[] player2 = new int[5];
Player somePlayer = new Player(player2);

1 Comment

The declared constructor has a parameter of type int[] not int.
0

Your only constructor takes an int array as argument, so that's why.

If you think it's ok to initialize a Player object without the int array, you can add a no-argument constructor, i.e like this:

public Player(){}

Or you will have to pass an int array to the constructor, like this:

int[] tmpa=null;
Player p=new Player(tmpa);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.