Results 1 to 10 of 10
  1. #1
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Can't fill the array with the object?

    This is frustrating...My array keeps getting filled with the address object, and not the contents of the object, like so:



    I can't figure out what I'm doing wrong...

    Main:
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class CSCD210Lab9
    {
    	public static void main(String [] args)throws IOException
    	{
    		int total, choice;
    		Address [] addresses = null;
    
    		Scanner fin = new Scanner(new File("addresses.txt"));
    		/* An address in the file is
    		street - String
    		city - String
    		state - String
    		zip - int
    		*/
    		total = FileUtilLab9.count(fin);
    		fin.close();
    
    		fin = new Scanner(new File("addresses.txt"));
    
    		addresses = FileUtilLab9.fillArray(fin, total);
    		fin.close();
    fillArray:
    Java Code:
    	public static Address [] fillArray(Scanner fin, int total)
    	{
    		String st = fin.nextLine();
    		String ci = fin.nextLine();
    		String sta = fin.nextLine();
    		int z = Integer.parseInt(fin.nextLine());
    		//Address[] myAddress = new Address[total];
    		Address temp = new Address(st, ci, sta, z);
    		Address[] myAddress = new Address[total];
    		for(int x = 0; x < myAddress.length; x++)
    		{
    			myAddress[x] = temp; 
    		}//end for loop
    		
    		return myAddress;
    		*/
    	}//end fillArray
    Address:
    Java Code:
    public class Address
    {
    	private String street = null;
    	private String city = null;
    	private String state = null;
    	private int zip = 0;
    	
    	public Address(String st, String ci, String sta, int z)
    	{
    		this.street = st;
    		this.city = ci;
    		this.state = sta;
    		this.zip = z;
    	}//end Address
    	
    }//end class

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Make some methods in the Address class so you can access its private variables. The Addresses in your array can then be accessed and call the said methods.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Another option, provide a toString method.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    By the way you are filling the array with the same object.

  5. #5
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Yeah, that's what I'm trying to avoid... I just want to fill the array with the object a single time but can't figure it out.

    I've been working on this too much. :(

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Huh?

    Do you want every element of the array to be the same object and when you change one they all change.

    Do you want every element of the array to be the same object and when you change one the rest stay unchanged.

    Do you want every element of the array to be different.

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

    Default

    Use your Scanner object to extract information from the file and create a new Address object inside the for loop.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Clerek View Post
    Yeah, that's what I'm trying to avoid... I just want to fill the array with the object a single time but can't figure it out.

    I've been working on this too much. :(
    .................
    Last edited by al_Marshy_1981; 02-21-2011 at 12:22 AM. Reason: Junky covered all the questions

  9. #9
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    I want every element of the array to be different and independent objects. When I change one the rest stay unchanged.

    Like this? The thing I was missing was the total/4. there are 4 elements in each object. Running it through wasn't working because it was putting the same element into the array. Now it can read the other addresses in the addresses.txt file! You all ARE GENIUS!!!

    Java Code:
    	public static Address [] fillArray(Scanner fin, int total)
    	{
    		Address[] myAddress = new Address[total/4];
    		for(int x = 0; x < myAddress.length; x++)
    		{
    			String st = fin.nextLine();
    			String ci = fin.nextLine();
    			String sta = fin.nextLine();
    			int z = Integer.parseInt(fin.nextLine());
    			Address temp = new Address(st, ci, sta, z);
    			myAddress[x] = temp; 
    		}//end for loop
    		
    		return myAddress;
    
    	}//end fillArray
    Here's the pics of the arrays using the debuggerL


  10. #10
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    And it makes sense now that I see it...

    I need to take a break! :)

Similar Threads

  1. how to fill and show an array with graphic components?
    By randomjavaguy in forum New To Java
    Replies: 2
    Last Post: 11-08-2010, 09:19 AM
  2. fill object of array with char
    By begginer12 in forum New To Java
    Replies: 1
    Last Post: 12-05-2009, 02:11 AM
  3. (Trying to) fill HSB (Hue|Saturation|Brightness) array
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 07:49 AM
  4. Replies: 0
    Last Post: 04-23-2008, 08:15 PM
  5. Array Fill Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:45 PM

Posting Permissions

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