Results 1 to 5 of 5
Thread: Array of objects
- 09-24-2011, 05:58 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Array of objects
I am trying to create an array of objects, the objects are made in a separate class (CreateObject) with getters and setters and such, and I have tested that code and it all works the way I want it to. However I am having issues trying to work out the best way to store those individual objects in my array. Any help on the matter would be greatly appreciated. I have added what I believe to be the relevant code below.
I keep getting back this error:PHP Code:private static void processLine(String csvRow, int lineNum) { /************************************** * Using split() parsing known-format * **************************************/ // Would be good to split csvRow in CreateObject String key, brand, model, weight, price, strKey, strBrand, strModel; int intWeight; double dblPrice; String[] tokens; tokens=csvRow.split(","); key=tokens[0]; brand=tokens[1]; model=tokens[2]; weight=tokens[3]; price=tokens[4]; System.out.println("Key:"+key+" Brand:"+brand+" Model:"+model+" Weight:"+weight+" Price:"+price); //Use CreateObject strKey=new CreateObject(key, brand, model, weight, price).getKey(); strBrand=new CreateObject(key, brand, model, weight, price).getBrand(); strModel=new CreateObject(key, brand, model, weight, price).getModel(); intWeight=new CreateObject(key, brand, model, weight, price).getWeight(); dblPrice=new CreateObject(key, brand, model, weight, price).getPrice(); //System.out.println("strKey:"+strKey+" strBrand:"+strBrand+" strModel:"+strModel+" intWeight:"+intWeight+" dblPrice:"+dblPrice); loadArray(strKey, strBrand, strModel, intWeight, dblPrice, lineNum); } private static void loadArray(String strKey, String strBrand, String strModel, int intWeight, double dblPrice, int lineNum) { CreateObject[] array; array=new CreateObject[5]; array[lineNum-1]=new CreateObject(strKey, strBrand, strModel, intWeight, dblPrice); System.out.println("Index 0: "+array[0]+" index 1: "+array[1]+" index 2: "+array[2]+" index 3: "+array[3]+" index 4: "+array[4]); }
where line 274 is the 3rd last line.LoadRecordsIntoArray.java:274: cannot find symbol
symbol : constructor CreateObject(java.lang.String,java.lang.String,jav a.lang.String,int,double)
location: class CreateObject
array[lineNum-1]=new CreateObject(strKey, strBrand, strModel, intWeight, dblPrice);
^
1 error
At one point I tried splitting line in CreateObject and it worked to an extent, but in my System.out.println (the last line of the code) it kept giving me this:
I thought maybe that was a good thing and I was just missing how to get the value at what appears to be an address?Enter file name:
DataFive.csv
Index 0: CreateObject@eb42cbf index 1: null index 2: null index 3: null index 4: null
Index 0: null index 1: CreateObject@56e5b723 index 2: null index 3: null index 4: null
Index 0: null index 1: null index 2: CreateObject@35a8767 index 3: null index 4: null
Index 0: null index 1: null index 2: null index 3: CreateObject@2c6f7ce9 index 4: null
Index 0: null index 1: null index 2: null index 3: null index 4: CreateObject@4b71bbc9
- 09-24-2011, 06:15 PM #2
Re: Array of objects
Does the CreateObject class have a constructor that matches the variable types used as parameters in the statement causing the error?cannot find symbol
symbol : constructor CreateObject(java.lang.String, java.lang.String, java.lang.String, int,double)
That String is what is returned by the Object class's default toString() method.CreateObject@4b71bbc9
Override the toString() method in your CreateObject class and have it return a String with what you'd like to see.
- 09-26-2011, 03:24 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Re: Array of objects
Wow, thanks for the speedy reply!
My object doesn't have a toString method or require one. This is the code I'm using to load the objects into the array (I made a few changes from the other day)
So I (think) I'm creating an array of length 5 of CreateObject objects. lineNum starts at 1 so to get index zero in the array I minus 1, and it increases by increments, though that's not visible here. Then for each index 0 to 4 I am inserting one object. line also changes each time with lineNum so there is different info in each line but always of the same format.Java Code:private static void loadArray(String line, int lineNum) { CreateObject[] array; array=new CreateObject[5]; array[lineNum-1]=new CreateObject(line); System.out.println("Index 0: "+array[0]+" index 1: "+array[1]+" index 2: "+array[2]+" index 3: "+array[3]+" index 4: "+array[4]); }
How do I tell if the object in each index of the array is the one I want it to be?
Thank you for your help
- 09-26-2011, 03:41 PM #4
Re: Array of objects
One problem with what you have posted is that you have the definition for array inside of a method.
When the method exits, the array will disappear. You need to move the definition outside of the method to the class level so that it will remain in existence between method calls.
It is up to you to put the objects into the array at the location you want them to be.How do I tell if the object in each index of the array is the one I want it to be?
How do you want to access the elements in the array? What relationship is there between their location in the array and their values?
- 09-30-2011, 01:27 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
Array of Objects
By sfe23 in forum New To JavaReplies: 19Last Post: 02-04-2009, 05:57 PM -
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks