Welcome to Dream.In.Code
Become a Java Expert!

Join 150,383 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,178 people online right now. Registration is fast and FREE... Join Now!




Java

 
Reply to this topicStart new topic

Java, Reading from a text file into an array of objects

chetah
7 Feb, 2008 - 06:58 AM
Post #1

D.I.C Head
**

Joined: 17 Nov, 2007
Posts: 61



Thanked: 1 times
My Contributions
CODE

input file "employee.txt"

234 Brian Robertson 678.00
566 Chris Robson 678.00
567 Miltion Small 567.00
0



import java.io.*;
import java.util.*;

class Employee{
    private int idnum;
    private String fname;
    private String lname;
    private double salary;
    
    Employee(int id, String fn, String ln, double sal){
        idnum = id;
        fname = fn;
        lname = ln;
        salary = sal;
    }
    
    
    public void setId(int id){
        idnum = id;
    }
    
    public void setFname(String fn){
        fname = fn;
    }
    
    public void setLname(String ln){
        lname = ln;
    }
    
    public void setSalary(double sal){
        salary = sal;
    }
    
    public int getId(){
        return idnum;
    }
    
    public String getFname(){
        return fname;
    }
    public String getLname(){
        return lname;
    }
    public double getSalary(){
        return salary;
    }
    
}


class TestRead{
    public static void main(String[] args)throws IOException{
        Employee[] array = new Employee[100];
        Scanner in = new Scanner(new FileReader("employees.txt"));
        
        int i = 0;
        int v = in.nextInt();
        while(v!=0){
            array[i]=new Employee(in.nextInt(),in.next(),in.next(),in.nextDouble());
            i++;
        }
        
        //Print array
        for(int j = 0; j < array.length; j++){//Exception in thread "main" java.util.InputMismatchException
            System.out.println(array[j]);
        }
        
    }
}



Description of Problem
I am trying to read the file at the beginning into an array of objects. When I compile the code it is ok, but when I try to print it indicated error "Input type mismatch". My suspiscions are it has to do with the Scanner object(reading the values in). How can I rectify this problem? Is there an alternative way?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Java
7 Feb, 2008 - 07:26 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
The first time you call nextInt() grabs the "234" before Brian, and then when you call it in your constructor, it tries to read in "Brian" and convert that into an int.
I would do this with your while loop:
CODE

while ( (v = in.nextInt() ) != 0)
    array[i++]=new Employee(v,in.next(),in.next(), in.nextDouble() );

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month