Results 1 to 9 of 9
  1. #1
    sadeem is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default need to read information from file into array

    hi
    i need to create contact information class which is store the information from file to an array or (any collection).
    the file include the first name and last name like this
    Sadeem Shizan
    Alban Alsha
    and so on..

    so what i do ..
    Java Code:
    public class Contact{
    private String Fname;
    private String Lname;
    private int id;
        public Contact(int id,String fname,String lname) {
        Fname=fname;
        Lname=lname;
        this.id=id;	
        }
        
    }
    ...
    Java Code:
    public class ContacInformation {
    
     public ContacInformation() {
      
        }
     public static void main(String[] args) throws IOException{
     	String fname,lname;
     	int id;
     	int i=0;
     	int d=1;
     	Contact[] contact=new Contact[30];
     	Scanner scan=new Scanner ("contactInfo.txt");
     	while(scan.hasNext()){
     		fname=scan.next();//read first word 
     		lname=scan.next();//read second word
     		id=d;
     		d++;
     		contact[i]=new Contact(id,fname,lname);
     		i++;
     		
     	}
     	for(int j=0;j<contact.length;j++){
     		System.out.println(contact[i]);
     	}
     	
     }   
        
    }
    i can't get the output, i don't know where is my wrong

    can you help me :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you get no output, or just weird output? If it's weird output would you mind showing the output?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    You need to provide a method in the Contact class to print out the contents of the class object. You are probably seeing an internal representation of the class object (the name and address perhaps).
    One way is to override the Object class's toString() method and format the output the way you want to see.

  4. #4
    sadeem is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    sunde887
    i get an exception ..i attach the output

    norm

    are u mean like this
    Java Code:
    	for(int j=0;j<contact.length;j++){
     		System.out.println(contact[i].toString());
    :confused::confused:
    Attached Thumbnails Attached Thumbnails need to read information from file into array-output.jpg  

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Your exception is because the loop variable and the index variable are NOT the same.

    My comment was to override the toString method for the Contact class. By that I mean add a method/code to the Contact class that returns what you want to see when printing.

  6. #6
    sadeem is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    i do this edit but the exception is still occure

    Java Code:
    public class Contact{
    private String Fname;
    private String Lname;
    
    public Contact(){
    }
        public Contact(String fname,String lname) {
        Fname=fname;
        Lname=lname;
       
        }
        
    
    
        public String getFname()
        {
           return Fname;
        }
         public String getLnname()
        {
           return Lname;
        }
          public String toString()
        {
           return getClass().getName()
              + "[name=" + Fname;
              
        }
    }
    >>
    Java Code:
    public ContacInformation() {
      
        }
     public static void main(String[] args) throws IOException{
     	String fname,lname;
     	int id;
     	int i=0;
     	
     	Contact[] contact=new Contact[30];
     	Scanner scan=new Scanner ("contactInfo.txt");
     	while(scan.hasNext()){
     		fname=scan.next();//read first word 
     		lname=scan.next();//read second word
     		
     		
     		contact[i]=new Contact(fname,lname);
     		i++;
     		
     	}
    
     	}
     	
     }
    Attached Thumbnails Attached Thumbnails need to read information from file into array-o.png  

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    What is the source code at line 25?

    Add a print statement after each next() call to show what was read. You'll see where the error is.


    You are reading two lines with the next() method but you do NOT test with hasNext() before the second one.
    Last edited by Norm; 05-15-2011 at 09:28 PM.

  8. #8
    sadeem is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default what should i do to solve it please help me

    ok, see when i do this

    Java Code:
    public class ContacInformation {
    
     public ContacInformation() {
      
        }
     
     	public Contact[] readinfo()throws IOException{
     	
     	String fname,lname;
     	
     	int i=0;
     	
     	Contact[] contact=new Contact[30];
     	Scanner scan=new Scanner ("contactInfo.txt");
     	while(scan.hasNext()){
     		fname=scan.next();//read first word 
     		
     		
     		
     		contact[i]=new Contact(fname);
     		i++;
     		
     	}
    return contact;
     	}
     	
      public static void main(String [] args) throws IOException{
      	ContacInformation cinf=new ContacInformation();
      	Contact c[]=null;
      	c=cinf.readinfo();
      	for(int i=0;i<c.length;i++)
      		System.out.println(c[i]);
      } 	
     }

    why i get this output :confused::confused::mad:
    Attached Thumbnails Attached Thumbnails need to read information from file into array-ou.png  

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    What do you see in the first line that is printed out? What does that tell you?

    You have changed the program so that there is only one call to next(). So now hasNext() does its job.

Similar Threads

  1. read input file into array
    By randoms:) in forum New To Java
    Replies: 3
    Last Post: 04-23-2011, 08:52 AM
  2. Help needed: read array from file and then use it
    By ChicagoAve in forum New To Java
    Replies: 11
    Last Post: 02-18-2011, 01:02 PM
  3. Read File into 2d array
    By almjodla in forum New To Java
    Replies: 8
    Last Post: 03-23-2010, 02:55 PM
  4. Replies: 2
    Last Post: 11-29-2009, 10:26 AM
  5. Replies: 3
    Last Post: 04-07-2008, 06:02 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
  •