0

I'm declate the multidimensional array propertyArray = new int[numbHuman][40]

where [numbHuman] is the number of rows, and the [40] is the number of columns (or vice versa doesnt really matter). Anyway I create those propertyArray's in a nested for loop, but I'm not sure How I'd pass propertyArray[][] into the player object constructor. Here is my code, I will try to clarify if needed.

import java.util.Scanner;
import java.util.Random;
public class PlayerArray
{
    Scanner scan = new Scanner(System.in);
    private int numbHuman;
    private Player[] arr;
    private String[] userName;
    //private 
    private int[] testArray;
    private int[][] propertyArray;
    private int[] userID;
    private int startingMoney;
    private int startingPosition;
    private int b;
    private int c;
    private int i;
    //private int d;
    public PlayerArray()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("How many players wish to play? Values of 2 through 8 accepted.");
        numbHuman = scan.nextInt();
        System.out.println(numbHuman + " players selected.");
        while (numbHuman < 2 || numbHuman > 8)
        {
            System.out.println("Invalid entry, try again.");
            numbHuman = scan.nextInt();
        }       
        arr = new Player[numbHuman];
        i=0;
        testArray = new int[40];
        propertyArray = new int[numbHuman][40];///// WORK ON DIS 
        userName = new String[numbHuman];
        userID = new int[numbHuman];
        startingMoney = 1500;
        startingPosition = 0;
        b=0;
        c=0;
        for(int i = 0; i < arr.length; i++)
        {

            userID[i] = b;
            System.out.println("Player " + (i + 1) + ", Please enter your first name:");
            userName[i] = scan.next();

            for(c = 0; c < 40; c++)
            { 
                propertyArray[i][c] = 0;
            }

            arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition,propertyArray[i][c]);
            b++;
        }
    }

I'm trying to pass it into, how do i go about this? All the other variables work but the multidimensional array.

public Player(String userName, int changeInMoney, int userID, int startingPosition, int[][]propertyArray)
    {
        myID = userID;
        myName = userName;
        currentPosition += startingPosition;
        currentBal -= payBank(changeInMoney);
    }
2
  • Side note: it might be better not to provide all that input prompting inside the array class itself. Commented Dec 3, 2013 at 1:32
  • EDIT just updated last lines of First class. Commented Dec 3, 2013 at 1:44

2 Answers 2

1

You need to specify a set of square brackets for each dimension, e.g.:

public Player(String userName, int changeInMoney, int userID, 
    int startingPosition, int[][] propertyArray)
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry I accidently left out a small portion of significant code, it should be arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition, propertyArray[i][c]);
@BrothahHeffay it makes no difference, you need to redefine the constructor to match the prototype above in order to accept a two dimensional array.
Oh sorry, I did that as well, let me update my code again XD And it still won't compile, complains that "actual argument int cannot be converted to int[][] by method invocation conversion"
@BrothahHeffay Sorry, re-reading your line of code, you're not actually passing a two dimensional array, propertyArray[i][c] is a single element (i.e. an int). Maybe you mean to call this as: arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition, propertyArray);
My intentions are to give each Player a corresponding array, where each index contains a 0. I'm starting to think I'm approaching the problem wrong...
0

Just add an extra pair of brackets:

public Player(String userName, int changeInMoney, int userID, int startingPosition, int[][] propertyArray)

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.