Results 1 to 5 of 5
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default HELP: Still having trouble getting arrays :(

    I'm still having trouble getting my array from one class into another class and can't figure out what I'm doing wrong.

    I've included part of the code below and highlighted the sections in red where I'm having trouble.

    I'm trying to read an element (CardIDDuringGame) from the array silverAndGoldDecks, which I created and populated in the class CardArray, while I'm in the class MapArray. I need to modify the array map based on values in one of the elements of silverAndGoldDecks.

    How can I do this correctly?

    Java Code:
    public class Main
    {
        public static void main(String[] args)
                throws FileNotFoundException, BiffException, IOException, WriteException
        {
            Random randomGenerator = new Random();
            int randomMap = randomGenerator.nextInt(695)+1;
    
            MapArray mapArray = new MapArray();
            CardArray masterList = new CardArray();
            CardArray silverAndGoldDeck = new CardArray();
    
            int castlePosition = mapArray.buildMapArray(randomMap);
            masterList.buildMasterArray();
            silverAndGoldDeck.buildSilverAndGoldArrays(1,2,castlePosition);
            [B][COLOR="Red"]mapArray.addDeckPositionsToMap();[/COLOR][/B]
    
            mapArray.displayMapArray();
            masterList.displayMasterArray();
            silverAndGoldDeck.displaySilverArray();
            silverAndGoldDeck.displayGoldArray();
        }
    }
    Java Code:
    public class MapArray
    {
        int silverGoldCastlePositions = 0;
        MapProperties[] map = new MapProperties[501];
        CardArray silverAndGold = new CardArray();
    
        public MapArray()
            {
            for(int i=0; i < map.length; i++)
                {
                map[i] = new MapProperties();
                }
            }
    
    
        public int buildMapArray(int mapNumber) throws IOException, BiffException, WriteException
            {
            File inputWorkbook = new File("Map.xls");
    	Workbook workbook;
            try
                {
                workbook = Workbook.getWorkbook(inputWorkbook);
                Sheet mapSheet0 = workbook.getSheet(0);
                int x = 0;
                for(int i = 0; i < 501; i++)
                    {
                    if(i < 86)
                        {
                        Cell cellValue0 = mapSheet0.getCell(i+6, mapNumber);
                        x = Integer.valueOf(cellValue0.getContents()).intValue();
                        if(x == 9)
                            {
                            map[i].setCardOccupying(100);
                            System.out.println("Silver Castle i = "+(i+1)*100);
                            silverGoldCastlePositions += (i+1)*100;
                            }
                        else if(x == 10)
                            {
                            map[i].setCardOccupying(200);
                            System.out.println("Gold Castle i = "+(i+1));
                            silverGoldCastlePositions += i+1;
                            }
                        }
                    else
                        x = 0;
                    map[i].setMapPosition(i+1);
                    map[i].setTerrainType(x);
                    }
                }
            catch (BiffException e)
                {
                e.printStackTrace();
                }
            return silverGoldCastlePositions;
    	}
    
    [COLOR="Red"][B]    public void addDeckPositionsToMap()
            {
    [COLOR="SeaGreen"]        // how do i get getSilverAndGoldDecks?[/COLOR]
            CardProperties[] silverAndGold = new CardProperties();  // doesn't work
            silverAndGold.getSilverAndGoldDecks();  // doesn't work
    [COLOR="SeaGreen"]        // Modify getSilverAndGoldDecks[/COLOR]
            }[/B][/COLOR]
    
    
        public int[] getMapTerrain()
            {
            int[] terrain = new int[86];
            for(int i = 0; i < 86; i++)
                {
                terrain[i] = map[i].getTerrainType()+47*i;
                System.out.println("map pos = "+map[i].getMapPosition());
                }
            return terrain;
            }
    
        public void displayMapArray()
            {
            for(int i = 0; i < map.length; i++)
                {
                System.out.println(map[i].getMapProperties());
                }
            }
    
    }
    Java Code:
    public class CardArray
    {
        int silverGoldCastlePositions;
    
        CardProperties[] card = new CardProperties[506];
        CardProperties[] silverAndGoldDecks = new CardProperties[200];
        int[] mapPositions = new int[86];
    
        public CardArray()
            {
            for(int i=0; i < card.length; i++)
                {
                card[i] = new CardProperties();
                }
            for(int i=0; i < silverAndGoldDecks.length; i++)
                {
                silverAndGoldDecks[i] = new CardProperties();
                }
            }
    
        ......
     
        public void buildSilverAndGoldArrays(int player1, int player2, int castlePosition)
                                     throws IOException, BiffException, WriteException
            {
            File inputMasterListWorkbook = new File("Master List.xls");
            File inputPlayerListWorkbook = new File("Player List.xls");
    	Workbook workbookMasterList;
            Workbook workbookPlayerList;
    
    	try
     
           .......
    
    [COLOR="SeaGreen"]       // build array named silverAndGoldDecks[/COLOR]
    
           .......
    
            catch (BiffException e)
                {
                e.printStackTrace();
                }
    	} 
     
        public CardProperties[] getSilverAndGoldDecks()
            {
            return this.silverAndGoldDecks;
            }
    
    }
    Java Code:
    public class CardProperties
    {
        public int cardIDDuringGame;
        ...
        public int position;
    
    
        public void setCardIDDuringGame(int CardIDDuringGame)
            {
            cardIDDuringGame = CardIDDuringGame;
            }
        ...
        public void setPosition(int Position)
            {
            position = Position;
            }
    
    
        public int getCardIDDuringGame()
            {
            return cardIDDuringGame;
            }
        ...
        public int getPosition()
            {
            return position;
            }
    }
    Java Code:
    public class MapProperties
    {
        public int mapPosition;
        public int terrainType;
        public int cardOccupying;
    
    
        public void setMapPosition(int MapPosition)
            {
            mapPosition = MapPosition;
            }
        public void setTerrainType(int TerrainType)
            {
            terrainType = TerrainType;
            }
        public void setCardOccupying(int CardOccupying)
            {
            cardOccupying = CardOccupying;
            }
    
    
        public int getMapPosition()
            {
            return mapPosition;
            }
        public int getTerrainType()
            {
            return terrainType;
            }
        public int getCardOccupying()
            {
            return cardOccupying;
            }
        public String getMapProperties()
            {
            return mapPosition + "   " + terrainType + "   " + cardOccupying;
            }
    }

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Java Code:
    public void addDeckPositionsToMap()
            {
            // how do i get getSilverAndGoldDecks?
            CardProperties[] silverAndGold = new CardProperties[];  // doesn't work
            silverAndGold.getSilverAndGoldDecks();  // doesn't work
            // Modify getSilverAndGoldDecks
            }
    I have chaged this line [code]CardProperties[] silverAndGold = new CardProperties[]; // doesn't work[/code/]
    instead of () i changed []...try that

    EDIT:

    Sorry my mistake...didnt read the code properly. *Checking again*
    Last edited by FlyNn; 02-05-2010 at 06:57 PM. Reason: Change

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure what exactly your program is trying to do, but one way to possibly have shared references is to pass a CardArray object to MapArray via a constructor parameter like so:

    Java Code:
    public class MapArray
    {
        int silverGoldCastlePositions = 0;
        MapProperties[] map = new MapProperties[501];
        CardArray silverAndGold;
    
        public MapArray(CardArray silverAndGold)
        {
            this.silverAndGold = silverAndGold;
            for(int i=0; i < map.length; i++)
                {
                map[i] = new MapProperties();
                }
        }

    Then in the main method, you create the CardArray object first, and pass it to MapArray.
    Java Code:
    public class Main
    {
        public static void main(String[] args)
                throws FileNotFoundException, BiffException, IOException, WriteException
        {
            Random randomGenerator = new Random();
            int randomMap = randomGenerator.nextInt(695)+1;
    
            //MapArray mapArray = new MapArray();
            CardArray masterList = new CardArray();
            CardArray silverAndGoldDeck = new CardArray();
            MapArray mapArray = new MapArray(silverAndGoldDeck);
    Last edited by Fubarable; 02-05-2010 at 10:36 PM. Reason: corrected one error in code

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Fubarable,

    Quote Originally Posted by Fubarable View Post
    I'm not sure what exactly your program is trying to do, but one way to possibly have shared references is to pass a CardArray object to MapArray via a constructor parameter like so:

    Java Code:
    public class MapArray
    {
        int silverGoldCastlePositions = 0;
        MapProperties[] map = new MapProperties[501];
        CardArray silverAndGold;
    
        public MapArray(CardArray silverAndGold)
        {
            this.silverAndGold = silverAndGold;
            for(int i=0; i < map.length; i++)
                {
                map[i] = new MapProperties();
                }
        }

    Then in the main method, you create the CardArray object first, and pass it to MapArray.
    Java Code:
    public class Main
    {
        public static void main(String[] args)
                throws FileNotFoundException, BiffException, IOException, WriteException
        {
            Random randomGenerator = new Random();
            int randomMap = randomGenerator.nextInt(695)+1;
    
            //MapArray mapArray = new MapArray();
            CardArray masterList = new CardArray();
            CardArray silverAndGoldDeck = new CardArray();
            MapArray mapArray = new MapArray(silverAndGoldDeck);
    THANK YOU! THANK YOU! THANK YOU!

    This seems to have solved my problem. I'm finally able to pass my arrays into my GUI and can now begin working on the GUI further.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. arraylist trouble
    By Reiyn in forum New To Java
    Replies: 9
    Last Post: 05-28-2009, 12:03 AM
  2. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 04:18 PM
  3. Got some trouble with JComboBox
    By hungleon88 in forum AWT / Swing
    Replies: 16
    Last Post: 09-15-2008, 12:26 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 AM
  5. HELP: Trouble with partial filled arrays
    By daigre7 in forum New To Java
    Replies: 1
    Last Post: 04-07-2008, 03:05 AM

Posting Permissions

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