0

I'm creating the game of monopoly, star wars edition for fun, not for homework :) Anyways I'm prompting the user to input how many 'human' players they want out of 4 players in total. Then I use a for loop to create that amount of human players. That seems to work however when I use a getter method to test and see if the players survive outside the loop, it says "cannot find symbol - variable player." This was my first attempt at using arrays as well. So If someone could assist me on accessing and using those created unique player objects, that would be great.

    import java.util.Scanner;
import java.util.Random;
public class Launcher
{
    PairOfDice myDice = new PairOfDice();
    private Planet myTest;
    private PlanetInfo myPlanetInfo;
    public static void main(String[] args)
    {

        Scanner scan = new Scanner(System.in);
        System.out.println("You are in the presence of the Naked Jeff's code. Please, be humbled.");
        System.out.println();
        System.out.println("There will be 4 players, how many do you wish to be human? 0><4");

        ////////////////////////////////////////////////////////// HOW TO CREATE MULTIPLE OBJECTS BASED ON LOOP
        int numbHuman = scan.nextInt();
        while (numbHuman < 1 || numbHuman > 4)
        {
            System.out.println("Invalid entry, try again.");
            numbHuman = scan.nextInt();
        }
        Player[] arr = new Player[numbHuman];
        String[] userName = new String[numbHuman];
        for(int i = 0; i < arr.length; i++)
        {
            System.out.println("Player " + (i + 1) + ", Please enter your first name:");
            userName[i] = scan.next();
            arr[i] = new Player(userName[i]);
        }
        System.out.println("player " + (4) + "'s name is" + [COLOR="#FF0000"]player[/COLOR][3].getName());
        /////////////////////////////////////////////////////////
    }
}


//and here's my Player class.

public class Player
{
    PairOfDice myDice = new PairOfDice();
    private int startingBal;
    private int currentBal;
    private String myName;
    private int rollOne;
    private int rollTwo;
    private int doublesCount;
    private boolean roll;

    public Player(String userName)
    {
        myName = userName;
        System.out.println("Player's name is: " + myName);
    }
    public String getName(){return myName;}
    public int playerGo()
    {
        myDice.rollDice();
        rollOne = myDice.getResult1();
        rollTwo = myDice.getResult2();
        while(rollOne == rollTwo && roll == true)
        {
            doublesCount++;
            roll = false;
            //WORKING ON THIS//
        }
        return 0;
    }
    public boolean getGoAgain(boolean roll)
    {
        return roll;
    }
    public int newBalance()
    {

        return 1;
    }
}
4
  • [COLOR="#FF0000"]player[/COLOR][3].getName() that's probably ill formatted, or do you have that in your actual code? Commented Nov 28, 2013 at 16:57
  • could you show the exception thrown? Commented Nov 28, 2013 at 16:57
  • Where exactly is the error? You've presented a lot of code - can you cut it down to just a short but complete example showing nothing but the problem? Commented Nov 28, 2013 at 16:58
  • 1
    player[3] is not the player you're looking for. Move along.... Commented Nov 28, 2013 at 17:00

2 Answers 2

3

Instead of calling player[3].getName() you'd want to call arr[3].getName().

"Carefully read your code you must. Do use arr or do not. There is no player". ;)

Edit:

Btw, even arr[3].getName() might result in an error if there are less than 4 players. Use arr[numbHuman-1].getName() instead and be aware that this is not necessarily the 4th player.

So your line might instead be this:

System.out.println("player " + numbHuman + "'s name is " + arr[numbHuman-1].getName());
1
  • Duh! I'm trying to access the index of the array! Wow thanks for helping me I never would've figured it out. We haven't covered arrays in class so this is all new. That worked. Commented Nov 28, 2013 at 17:03
0

Replace

System.out.println("player " + (4) + "'s name is" + [COLOR="#FF0000"]player[/COLOR][3].getName()); 

with

System.out.println("player " +"'s name is" +arr[3].getName());

You have declared array of type Player and the name of that array is arr. So while accessing the array,you need to give the name of array with the index, so if you are accessing the third player and your index starts with 1, then code will be :

 arr[3].getName();

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.