Results 1 to 5 of 5
- 02-05-2010, 05:43 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
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; } }
- 02-05-2010, 05:55 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
I have chaged this line [code]CardProperties[] silverAndGold = new CardProperties[]; // doesn't work[/code/]Java Code:public void addDeckPositionsToMap() { // how do i get getSilverAndGoldDecks? CardProperties[] silverAndGold = new CardProperties[]; // doesn't work silverAndGold.getSilverAndGoldDecks(); // doesn't work // Modify getSilverAndGoldDecks }
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 05:57 PM. Reason: Change
-
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 09:36 PM. Reason: corrected one error in code
- 02-06-2010, 01:02 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
-
Similar Threads
-
arraylist trouble
By Reiyn in forum New To JavaReplies: 9Last Post: 05-27-2009, 11:03 PM -
Here comes trouble... :-)
By sargehendricks in forum IntroductionsReplies: 1Last Post: 04-23-2009, 03:18 PM -
Got some trouble with JComboBox
By hungleon88 in forum AWT / SwingReplies: 16Last Post: 09-15-2008, 11:26 AM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-21-2008, 11:52 PM -
HELP: Trouble with partial filled arrays
By daigre7 in forum New To JavaReplies: 1Last Post: 04-07-2008, 02:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks