Results 1 to 9 of 9
- 05-14-2011, 04:58 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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; } }
i can't get the output, i don't know where is my wrongJava 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]); } } }
can you help me :)
- 05-14-2011, 05:08 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Do you get no output, or just weird output? If it's weird output would you mind showing the output?
- 05-14-2011, 05:35 PM #3
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.
- 05-14-2011, 05:55 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
sunde887
i get an exception ..i attach the output
norm
are u mean like this
:confused::confused:Java Code:for(int j=0;j<contact.length;j++){ System.out.println(contact[i].toString());
- 05-14-2011, 05:58 PM #5
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.
- 05-15-2011, 09:17 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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++; } } }
- 05-15-2011, 09:22 PM #7
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.
- 05-15-2011, 09:49 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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:
- 05-15-2011, 10:00 PM #9
Similar Threads
-
read input file into array
By randoms:) in forum New To JavaReplies: 3Last Post: 04-23-2011, 08:52 AM -
Help needed: read array from file and then use it
By ChicagoAve in forum New To JavaReplies: 11Last Post: 02-18-2011, 01:02 PM -
Read File into 2d array
By almjodla in forum New To JavaReplies: 8Last Post: 03-23-2010, 02:55 PM -
Readin from a file. and storing the information. (read inside)
By Rock-On in forum New To JavaReplies: 2Last Post: 11-29-2009, 10:26 AM -
How would you get information from a file and then store it in an array?
By szimme101 in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 06:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks