Results 1 to 9 of 9
  1. #1
    thelastfoiter is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Creating multiple Objects using for loop.

    My problem is that I create the array with size based on user input (holds the different Player objects) but when I try accessing the index of the array (those objects) from another class or even a different object, I get the error "cannot find symbol - variable arr(the array at hand)" How can I gain access to the data in these Players? If my question is unclear just ask and I'll restate it. (error is in last line of PlayerOrder)

    Java Code:
    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]);
        }
    
    
    
        public void PlayerOrder()
        {
           myDice.rollDice();
           p1 = myDice.getResult1();
           p2 = myDice.getResult2();
           myDice.rollDice();
           p3 = myDice.getResult1();
           p4 = myDice.getResult2();
           System.out.println(p1 + " " + p2 + " " + p3 + " " + p4);
           System.out.println(arr[0].getName());
        }
    Last edited by thelastfoiter; 11-29-2013 at 01:10 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Creating multiple Objects using for loop.

    cannot find symbol - variable player
    Is the variable: player in scope (defined within same pair of {}s) as where it is being used?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    thelastfoiter is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Creating multiple Objects using for loop.

    YIKES I need more help lol, look bellow XD
    Last edited by thelastfoiter; 11-29-2013 at 01:09 AM.

  4. #4
    thelastfoiter is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Creating multiple Objects using for loop.

    My problem is that I create the array with size based on user input (holds the different Player objects) but when I try accessing the index of the array (those objects) from another class or even a different object, I get the error "cannot find symbol - variable arr(the array at hand)" How can I gain access to the data in these Players? If my question is unclear just ask and I'll restate it. (error is in last line of PlayerOrder)

    Java Code:
    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]);
        }
    
    
    
        public void PlayerOrder()
        {
           myDice.rollDice();
           p1 = myDice.getResult1();
           p2 = myDice.getResult2();
           myDice.rollDice();
           p3 = myDice.getResult1();
           p4 = myDice.getResult2();
           System.out.println(p1 + " " + p2 + " " + p3 + " " + p4);
           System.out.println(arr[0].getName());
        }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Creating multiple Objects using for loop.

    If you want to access arr[] in another method, then arr[] needs to be defined outside of any methods (ie at the class level).

    Read up on "scope of a variable".
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    thelastfoiter is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Creating multiple Objects using for loop.

    Alright I will, but the purpose is for the user to only create the amount of Players they want. Won't instantiating arr[] at class level make defeat this purpose? I'm unsure, and will read that section of my book. Thank you.
    NVM that worked! Thanks man!
    Last edited by thelastfoiter; 11-29-2013 at 01:36 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Creating multiple Objects using for loop.

    There is a difference between defining a variable
    and giving it a value.

    You can define a variable outside of any method and give it a value inside a method.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    thelastfoiter is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Creating multiple Objects using for loop.

    New problem, similar to my other one XD Thanks for helping me out btw.

    I've read the Oracle documents regarding scope and controlling access yet it just isn't sticking, so I'm assuming that my issue comes from my failure to understand... Anyways Here's my code. I'm trying to access the unique Player objects created in an array, and change their unique variables like their balances, using methods from the Player class. Any solutions and ESPECIALLY explanations are welcome!
    Java Code:
        public class Player
        {
        private int currentBal;
        private String myName;
        private int rollOne;
        private int rollTwo;
        private int rollTotal;
        private int doublesCount;
        private int currentPosition;
        private int currentDoubles;
        private int move;
        private int moveMult;
        private int newBal;
        private boolean rollAgain;
        private boolean inJail;
        public Player(String userName, int changeInMoney)
        {
            myName = userName;
            currentBal -= changeBalance(changeInMoney);
        }
    
        public int changeBalance(int changeInMoney){newBal -= changeInMoney; return newBal;}
    
        public int viewBalance(){return currentBal;}
    this is my player class, minus some methods I can't use till later. Bellow is my main method to call it,
    Java Code:
        public class Launcher
        {
        private Planet myTest;
        private PlanetInfo myPlanetInfo;
        private Player[] arr;
        public static void main(String[] args)
        {
            PlayerArray myArray = new PlayerArray();
            Pay myCharge = new Pay(); // continue work on charges 
            myArray.getPlayerArray();
            //STILL TRYING TO GET BELLOW LINE TO WORK LAST NIGHT!!!
            System.out.println("player 1's balance: " + /*here*/ arr[0].viewBalance());
        }
        }
    I'm getting the error 'non-static variable arr cannot be referenced to a static context.
    Could someone please explain why this is and explain a solution?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Creating multiple Objects using for loop.

    error 'non-static variable arr cannot be referenced to a static context.
    The variable: arr only exists inside of an instance of the Launcher class. The static main() method is not inside of any instance of the class.

    Why is there a Launcher class? Why does it contain variables if no instance is created?

    One solution would be to move the code from the main() method into the constructor for the Launcher class and have the main() method create an instance of the Launcher class with the new statement. Then the code in the constructor and the variable: arr would all be in an instance of the class.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help creating multiple objects
    By gamecock in forum New To Java
    Replies: 3
    Last Post: 09-29-2013, 04:44 PM
  2. How to add Multiple Objects in Frame
    By AryanArs in forum New To Java
    Replies: 5
    Last Post: 02-26-2012, 08:05 PM
  3. Multiple objects in a class help please
    By patcc2 in forum New To Java
    Replies: 12
    Last Post: 01-11-2011, 02:35 PM
  4. do we need multiple connection objects
    By Pacerier in forum Java Servlet
    Replies: 4
    Last Post: 11-27-2010, 06:16 PM
  5. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •