Results 1 to 7 of 7
  1. #1
    doranm09 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default repeatedly instantiating new objects and storing into an array.

    Hello,

    public void readZipCodeData(String filename) - open the provided filename and read all zip code data. Read one line at a time and separate the five data elements. Repeatedly, instantiate new ZipCodes and initialize with the five values. Insert each new ZipCode into the ArrayList.

    This is the description of what is needed.


    And this is what I have so far....

    Java Code:
        public void readZipCodeData(String filename){        
            String info;        
    
                try{
    						// open the data file
    					Scanner fileReader = new Scanner(new File(filename));
    					Scanner lineReader;
    
    					// continue while there is more data to read
    					while(fileReader.hasNext()) {
    
    						// read one line of data
    						info = fileReader.nextLine();
    
    						lineReader = new Scanner(info);
    						lineReader.useDelimiter(":");
    					
    						int pZip = lineReader.nextInt();
    						String pCity = lineReader.next();
    						String pState = lineReader.next();
    						double pLat = lineReader.nextDouble();
    						double pLong = lineReader.nextDouble();
    						
    						ZipCode position = new ZipCode(pZip, pCity, pState, pLat, pLong);
    						
    						zip_codes.add(position);
    						}
    
    				// could not find file
    					}catch(FileNotFoundException error) {
    					System.out.println("File not found ");
    
    				// problem reading the file
    				}catch(IOException error){
    					System.out.println("Oops!  Something went wrong.");
    				}
    			}
    When I compile it reaches the zip_codes.add(position) and spits back a compile error that reads "no suitable method found for add.(zipCode).

    Can someone please enlighten me on how I can instantiate new objects and place information from a text file into that objects all while storing them in an array list.
    Last edited by pbrockway2; 11-01-2011 at 06:49 AM. Reason: code tags added

  2. #2
    doranm09 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: repeatedly instantiating new objects and storing into an array.

    Oh and I already have a class of zipCodes, everything works fine up until this method.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: repeatedly instantiating new objects and storing into an array.

    When posting code use the "code" tags: put [code] at the start of the code and [/code]. This will allow the code to be readable. It is also a good idea to indent using (a few) spaces as tabs can be be excessively large when rendered by a web browser.

    When I compile it reaches the zip_codes.add(position) and spits back a compile error that reads "no suitable method found for add.(zipCode).
    Could you post the actual (full) compiler message? And the code where zip_codes is declared?

  4. #4
    doranm09 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: repeatedly instantiating new objects and storing into an array.

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    
    
    public class ZipCodeDatabase
    {       
        private ArrayList<ZipCode> zip_codes;
        
        public ZipCodeDatabase(){        
            zip_codes = new ArrayList<ZipCode>();        
        }
        
        public void readZipCodeData(String filename){        
            String info;        
    
                try{
    					// open the data file
    					Scanner fileReader = new Scanner(new File("ZipCodes.txt"));
    					Scanner lineReader;
    					// continue while there is more data to read
    					while(fileReader.hasNext()) {
    
    						// read one line of data
    						info = fileReader.nextLine();
    
    						lineReader = new Scanner(info);
    						lineReader.useDelimiter(",");
    					
    						int pZip = lineReader.nextInt();
    						String pCity = lineReader.next();
    						String pState = lineReader.next();
    						double pLat = lineReader.nextDouble();
    						double pLong = lineReader.nextDouble(); 
    						
    						ZipCode position = new ZipCode(pZip, pCity, pState, pLat, pLong);
    						
    						zip_codes.add(position);
    						}
    				// could not find file
    					}catch(FileNotFoundException error) {
    					System.out.println("File not found ");
    				// problem reading the file
    				}catch(IOException error){
    					System.out.println("Oops!  Something went wrong.");
    				}
    			}

    I figured it out, where the array list was declared for zip_codes I didn't declare the ZipCode object. If that makes sense??

  5. #5
    doranm09 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: repeatedly instantiating new objects and storing into an array.

    public ZipCode findZip(int zip) - if found, return the ZipCode that matches the provided zip code number. If not found, return null. Use a while loop and stop searching if a match is found.

    This is what I have so far...


    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    
    
    public class ZipCodeDatabase
    {       
        private ArrayList<ZipCode> zip_codes;
        
        public ZipCodeDatabase(){        
            zip_codes = new ArrayList<ZipCode>();        
        }
        
        public void readZipCodeData(String filename){        
            String info;        
    
                try{
    					// open the data file
    					Scanner fileReader = new Scanner(new File("ZipCodes.txt"));
    					Scanner lineReader;
    					// continue while there is more data to read
    					while(fileReader.hasNext()) {
    
    						// read one line of data
    						info = fileReader.nextLine();
    
    						lineReader = new Scanner(info);
    						lineReader.useDelimiter(",");
    					
    						int pZip = lineReader.nextInt();
    						String pCity = lineReader.next();
    						String pState = lineReader.next();
    						double pLat = lineReader.nextDouble();
    						double pLong = lineReader.nextDouble(); 
    						
    						ZipCode position = new ZipCode(pZip, pCity, pState, pLat, pLong);
    						
    						zip_codes.add(position);
    						}
    				// could not find file
    					}catch(FileNotFoundException error) {
    					System.out.println("File not found ");
    				// problem reading the file
    				}catch(IOException error){
    					System.out.println("Oops!  Something went wrong.");
    				}
    			}
    			
    	public ZipCode findZip(int zip){
    	    
    	    int i = 0;
    	    
    	    while(position.getZipCode() != zip && i < zip_codes.length){
    	        
    	        position = zip_codes.get(i);
    	        i++;
    	        
    	           if(position.getZipCode() != zip)
    	           
    	               return null ;
    	               
    	           }
    	   }
    essentially I just want to return the object that matches the int zip. However when I compile I receive an error that tells me "position cannot be found.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: repeatedly instantiating new objects and storing into an array.

    I'm guessing that the full error message mentioned line 55.

    The problem is that you have not declared position in that method. You must tell the compiler what the type of position is, and you must give it a non null value before you can usefully call one of its methods like getZipCode.

    Java Code:
    ZipCode position = ...
    In doing so you might want to rethink using a while loop. Your intention (I think) is to walk down the array testing zip codes: most straight forward would be a for loop. (for i=0 to whatever, get the position and test it). Or a "foreach" loop if you have come across these.

  7. #7
    doranm09 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: repeatedly instantiating new objects and storing into an array.

    This is what it looks like so far...

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    
    
    public class ZipCodeDatabase
    {       
        private ArrayList<ZipCode> zip_codes;
        
        public ZipCodeDatabase(){        
            zip_codes = new ArrayList<ZipCode>();        
        }
        
        public void readZipCodeData(String filename){        
            String info;        
    
                try{
    					// open the data file
    					Scanner fileReader = new Scanner(new File("ZipCodes.txt"));
    					Scanner lineReader;
    					// continue while there is more data to read
    					while(fileReader.hasNext()) {
    
    						// read one line of data
    						info = fileReader.nextLine();
    
    						lineReader = new Scanner(info);
    						lineReader.useDelimiter(",");
    					
    						int pZip = lineReader.nextInt();
    						String pCity = lineReader.next();
    						String pState = lineReader.next();
    						double pLat = lineReader.nextDouble();
    						double pLong = lineReader.nextDouble(); 
    						
    						ZipCode position = new ZipCode(pZip, pCity, pState, pLat, pLong);
    						
    						zip_codes.add(position);
    						}
    				// could not find file
    					}catch(FileNotFoundException error) {
    					System.out.println("File not found ");
    				// problem reading the file
    				}catch(IOException error){
    					System.out.println("Oops!  Something went wrong.");
    				}
    			}
    			
    	public ZipCode findZip(int zip){
    	    ZipCode positon = null;
    	    int i = 0;
    	    
    	    while(i<zip_codes.size() && !zip.equals(zip_codes.get(i).getZipCode())){
    	        i++;
    	    if(i<position.size())
    	       position = zip_codes.get(i);
    	    return position;
    
    	               
    	           }
    	   }
    }
    A for loop would make this much simpler however the requirements as stated above calls for a "while" loop, and I've done this in the past where I've created a student object and added the objects to an array list. But since I'm reading the zip codes from a text file, I want to make an ZipCode objects for each and every zip code. I'm not getting why my search method isn't working.

    When this particular code compiles I get an error message stating "int cannot be dereferenced".

    Here is my ZipCode class as well

    Java Code:
    public class ZipCode
    {
        private int zip_code;
        private String city;
        private String state;
        private double longitude;
        private double latitude;    
                
           public ZipCode(int pZip){
                
                zip_code = pZip;
                city = "UNKNOWN";
                state = "ST";
                longitude = 0.0;
                latitude = 0.0;
                
            }
            
            public ZipCode(int pZip, String pCity, String pState, double pLat, double pLong){
                
                zip_code = pZip;
                city = pCity;
                state = pState;
                latitude = pLat;
                longitude = pLong;
                
            }
            
            public void setZipCode(int pZip){
                
                zip_code = pZip;
                
            }
            
            public int getZipCode(){
                
                return zip_code;
                
            }
            
            public void setCity(String pCity){
                
                city = pCity;
                
            }
            
            public String getCity(){
                
                return city;
                
            }
            
            public void setState(String pState){
                
                state = pState;
                
            }
            
            public String getState(){
                
                return state;
                
            }
            
            public void setLatitude(double pLat){
                
                latitude = pLat;
                
            }
            
            public double getLatitude(){
                
                return latitude;
                
            }
            
            public void setLongitude(double pLong){
                
                longitude = pLong;
                
            }
            
            public double getLongitude(){
                
                return longitude;
                
            }
            
            public String toString(){
                
                String result = city +", " + state + ", " + zip_code;
                
                return result;
                
            }
            
    
            public static void main(String args[]){
            
                ZipCode position1 = new ZipCode(49444, "Norton Shores", "MI", 0.0, 0.0);
            
                System.out.println(position1);
                              
        }
    }

Similar Threads

  1. Storing objects in an array list?
    By Moedig in forum New To Java
    Replies: 3
    Last Post: 10-27-2011, 07:39 PM
  2. Storing objects
    By paul1024 in forum New To Java
    Replies: 5
    Last Post: 04-30-2011, 07:12 AM
  3. Storing array?
    By LifeElixer in forum New To Java
    Replies: 7
    Last Post: 04-14-2011, 07:10 AM
  4. Instantiating Multiple Graphics objects
    By theCardboardBox in forum New To Java
    Replies: 2
    Last Post: 11-25-2010, 02:26 PM
  5. Storing objects directly with db4o
    By german in forum JDBC
    Replies: 0
    Last Post: 05-12-2009, 09:22 PM

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
  •