Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default 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.

    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]);
    	}
    I keep getting back this error:

    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
    where line 274 is the 3rd last line.

    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:

    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
    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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Array of objects

    cannot find symbol
    symbol : constructor CreateObject(java.lang.String, java.lang.String, java.lang.String, int,double)
    Does the CreateObject class have a constructor that matches the variable types used as parameters in the statement causing the error?

    CreateObject@4b71bbc9
    That String is what is returned by the Object class's default toString() method.
    Override the toString() method in your CreateObject class and have it return a String with what you'd like to see.

  3. #3
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default 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)

    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]);
    	}
    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.

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default 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.

    How do I tell if the object in each index of the array is the one I want it to be?
    It is up to you to put the objects into the array at the location you want them 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?

  5. #5
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Array of objects

    I worked it out, you were right, there needed to be a toString method in my CreateObject class. I didn't quite understand what you were on about at first.

    Thanks for the help :)

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 PM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  4. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM

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
  •