Results 1 to 7 of 7
- 02-03-2010, 09:41 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
How do I search an array from a different class?
I have an array I want get grab information from. I've successfully got the array populated by reading in an Excel spreadsheet. The array is in its own class Players.java.
My array has 6 columns (elements?) and contains Player ID (key), Name, Rating, Games, Wins, Losses. I want to change some labels in a GUI when someone selects the player name from a Listbox.
For example,
ID____Name________Rating____Games____Wins____Losses
_1____Psyclone_____1631______125______71_______54
_2____Arkoos_______1793______114______74_______40
_3____Kellectra____1331_______97______38_______59
_4____Razan________1468______101______48_______53
When someone clicks on a name in the text box, their statistics (Rating, Games, Wins, Losses) get displayed in the UI. I already have some dummy text changing when someone clicks on a name from the list. I know how to get the value of the name selected in the list using getSelectedValue(). My problem is getting the values out of the array that is contained in a different class. I know how to search the array even... my problem is accessing it.
QUESTION: How do I read from the array in a different class?
I've been stuck on this problem for several days and have been searching for answers and just setting it aside while I worked on a different part of the program. But now I'm at a point where I'm pretty much stuck until I figure this out.
- 02-03-2010, 10:25 PM #2
Um do you mean get an array from a class? In the class, you can do either of the following:
1. Make the array public (not recommended because of encapsulation):
Java Code:public int[] arr;
Java Code:public int[] getArray() { return arr; }
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 02-03-2010, 10:34 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
It is an array of objects. I'm not sure if that changes things. What I'm trying to get is just 1 value from the array. For example...
System.out.println(card[1].getCardName());
When I run that line in one class is returns the name of the card. When I type that line in another class, it gives an error.
How do I get that line of code to work in a different class.
- 02-03-2010, 11:04 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
I made the public get method in the class I'm trying to read from...
Java Code:public CardProperties[] getArray() { return card; } }
I'm trying to read the array card in the class PlayerArrays.java. In the class PlayerArrays, I have tried...
Java Code:CardArrays cardArrays = new CardArrays(); CardProperties[] temp = cardArrays.getArray(); System.out.println(temp[1]);
I also tried...
Java Code:CardArrays cardArrays = new CardArrays(); CardProperties[] temp = cardArrays.getArray(); System.out.println(temp[1].getCardName());
Exception in thread "main" java.lang.NullPointerException
at Cards.PlayerArrays.setPlayerArrays(PlayerArrays.ja va:74)
at Cards.Main.main(Main.java:56)
Java Result: 1
- 02-03-2010, 11:21 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
OK, well I kinda got it to work. I made my CardArrays class return the array, then input the array as a parameter into the next class.
Is this the way it's supposed to be done?
-
Is the CardProperties[] array, cards, filled with CardProperties objects in the CardsArrays constructor?
- 02-04-2010, 12:21 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
Is the CardProperties[] array, cards, filled with CardProperties objects in the CardsArrays constructor?
I'll try to show you some of my code. Here is a portion of my CardArrays class. I deleted most of it because it's over 1000 lines long and had a lot of other methods.
Java Code:public class CardArrays { //Here are the declarations for the arrays CardProperties[] card = new CardProperties[506]; // Default Constructor builds the empty arrays public void ConstructCardArrays() { for(int i = 0; i < card.length; i++) card[i] = new CardProperties(); } // Set values for Master Array public void setMasterArray() throws IOException, BiffException, WriteException { File inputWorkbook = new File("Master List.xls"); Workbook workbook; try { workbook = Workbook.getWorkbook(inputWorkbook); Sheet sheet0 = workbook.getSheet(0); for(int i = 0; i < card.length; i++) { Cell cellValue0 = sheet0.getCell(0, i+1); int k0 = Integer.valueOf(cellValue0.getContents()).intValue(); Cell cellValue1 = sheet0.getCell(1, i+1); String k1 = String.valueOf(cellValue1.getContents()); ... Cell cellValue45 = sheet0.getCell(45, i+1); String k45 = String.valueOf(cellValue45.getContents()); card[i].setCardID(k0); card[i].setCardType(k1); ... card[i].setSpellText(k45); } } catch (BiffException e) { e.printStackTrace(); } }//End setMasterArray() //Displays the current values in the Master Array public void displayMasterArray() { for(int i = 0; i < card.length; i++) { System.out.println(card[i].getCardProperties()); } }//End displayMasterArray() public CardProperties[] getArray() { return card; } }
Java Code:public class PlayerArrays { //Here are the declarations for the arrays PlayersOnline[] playersOnline = new PlayersOnline[8]; // Default Constructor builds the empty array public void ConstructPlayerArrays() { for(int i = 0; i < playersOnline.length; i++) playersOnline[i] = new PlayersOnline(); } public PlayersOnline[] setPlayerArrays(CardProperties[] temp) throws IOException, BiffException, WriteException { // ***************************************************************************** System.out.println(temp[1].getCardName()); // ***************************************************************************** File inputWorkbook = new File("Player List.xls"); Workbook workbook; try { workbook = Workbook.getWorkbook(inputWorkbook); Sheet sheet0 = workbook.getSheet(0); for(int i = 0; i < sheet0.getRows()-1; i++) { Cell cellValue0 = sheet0.getCell(0, i+1); int k0 = Integer.valueOf(cellValue0.getContents()).intValue(); Cell cellValue1 = sheet0.getCell(1, i+1); String k1 = String.valueOf(cellValue1.getContents()); Cell cellValue2 = sheet0.getCell(3, i+1); int k2 = Integer.valueOf(cellValue2.getContents()).intValue(); Cell cellValue3 = sheet0.getCell(4, i+1); int k3 = Integer.valueOf(cellValue3.getContents()).intValue(); Cell cellValue4 = sheet0.getCell(5, i+1); int k4 = Integer.valueOf(cellValue4.getContents()).intValue(); Cell cellValue5 = sheet0.getCell(6, i+1); int k5 = Integer.valueOf(cellValue5.getContents()).intValue(); playersOnline[i].setPlayerID(k0); playersOnline[i].setPlayerName(k1); playersOnline[i].setPlayerRating(k2); playersOnline[i].setPlayerWins(k3); playersOnline[i].setPlayerLosses(k4); playersOnline[i].setPlayerCurrentDeck(k5); } } catch (BiffException e) { e.printStackTrace(); } return playersOnline; }//End setMasterArray() public void displayPlayerArrays() { for(int i = 0; i < playersOnline.length; i++) { System.out.println(playersOnline[i].getPlayersOnline()); } }//End displayDatabase() }
I temporarily solved the problem by returning card back to main (where I called it) and then used it as a parameter. For example, right now the code is running as
Java Code:public PlayersOnline[] setPlayerArrays(CardProperties[] temp) throws IOException, BiffException, WriteException
Java Code:public void setPlayerArrays() throws IOException, BiffException, WriteException
Similar Threads
-
Mostly completed array search program
By Ryujin89 in forum New To JavaReplies: 1Last Post: 11-04-2009, 03:25 AM -
Search a string in a byte array
By 2BOrNot2B in forum New To JavaReplies: 0Last Post: 03-12-2009, 05:52 PM -
[SOLVED] Search problem - array out of bounds
By viper110110 in forum New To JavaReplies: 5Last Post: 11-26-2008, 04:26 AM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM -
Array Search Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM
Bookmarks