简体   繁体   中英

Reading data from file (java)

I have a file that Im trying to collect data from so i can add the data to objects ive created but it doesnt seem to work and Im not sure why.

This is the code for trying to read the data:

    private void loadEmployeesFromFile(String fileName)
{
    File file = new File(fileName);
    Scanner scan = null;
    try {
        scan = new Scanner(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    int counter = 0;

    while(scan.hasNext())
    {
        scan.nextLine();
        counter++;
    }
    scan.close();

    employees = new Employee[counter];
    Scanner fileScan = null;
    try
    {
    fileScan = new Scanner(file).useDelimiter(",");
    }
    catch(FileNotFoundException e)
    {
        System.out.print("file was not found :/");
    }
    for(int i = 0; i < 3; i++ )
    {
        String id = fileScan.next();
        String firstName = fileScan.next();
        String lastName = fileScan.next();
        String hireDate = fileScan.next();
        int shift = fileScan.nextInt();
        double hourlyPayRate = 0;
        double bonusRate = 0;

        switch(id.substring(0, 2))
        {
        case "PW":
            if(fileScan.hasNextDouble())
                hourlyPayRate = fileScan.nextDouble();
            else if(fileScan.hasNextInt())
                hourlyPayRate = (double)fileScan.nextInt();
            employees[i] = new ProductionWorker(id, firstName, lastName, hireDate, shift, hourlyPayRate);
            break;
        case "TM":
            if(fileScan.hasNextDouble())
                hourlyPayRate = fileScan.nextDouble();
            else if(fileScan.hasNextInt())
                hourlyPayRate = (double)fileScan.nextInt();
            if(fileScan.hasNextDouble())
                bonusRate = fileScan.nextDouble();
            else if(fileScan.hasNextInt())
                bonusRate = (double)fileScan.nextInt();
            double reqTrainingHrs = fileScan.nextDouble();
            double trainingHrsDone = fileScan.nextDouble();
            employees[i] = new TeamLeader(id, firstName, lastName, hireDate, shift, hourlyPayRate, bonusRate, reqTrainingHrs, trainingHrsDone);
            break;
        case "SU":
            double annualSalary = fileScan.nextDouble();
            bonusRate = fileScan.nextDouble();
            employees[i] = new ShiftSupervisor(id, firstName, lastName, hireDate, shift, annualSalary, bonusRate);
            break;  
        }
    }

    fileScan.close(); 
}

and this the data file:

    PW_1234,James,Bond,01/02/10,1,10
    PW_1235,John,Brown,02/03/10,2,10.5
    PW_1236,Howard,Johnson,03/04/10,3,11
    PW_1237,Francis,Themule,04/05/11,4,10.75
    PW_1238,Mathew,Lewis,05/06/11,1,12.75
    PW_1239,Mark,Bixton,05/13/11,2,13
    PW_1242,Sarah,Glover,05/14/11,1,13.75
    PW_1245,John,Doe,05/15/11,4,10.5
    PW_1245,Mary,Doe,05/15/11,4,10.5
    TL_1248,Abel,English,05/16/11,3,16.5,0.01,100,89
    TL_1251,Eustis,Clauser,05/17/11,2,16,0.02,100,9
    SU_1254,Henry,Hollowman,05/18/11,1,40000,0.01
    PW_1240,Luke,Sailor,01/22/12,3,14.5
    PW_1243,Jane,Baker,01/23/12,2,14
    PW_1243,Jane,Baker,01/23/12,2,14
    TL_1246,David,Brief,01/24/12,1,14.75,0.01,100,57
    PW_1246,David,Doson,01/24/12,1,14.75
    TL_1249,Baker,Anderson,01/25/12,4,11.5,0.01,100,100
    TL_1252,Frank,Donson,01/26/12,3,17.5,0.02,100,39
    SU_1255,Issac,Asimov,01/27/12,2,43000,0.02
    SU_1256,Issac,Shoreman,01/28/12,3,39000,0.01
    SU_1257,Issac,Roberts,01/29/12,4,35500,0.01
    PW_1241,John,Candy,11/23/13,4,9.5
    PW_1244,Kate,Smith,11/24/13,3,15.5
    PW_1244,Kate,Handle,11/24/13,3,15.5
    TL_1247,Samual,Dempky,11/25/13,2,15,0.01,100,10
    TL_1250,Charley,Boman,11/26/13,1,15.75,0.01,100,50
    TL_1253,George,Fritzmen,11/27/13,4,12.5,0.02,100,27

For some odd reason when it gets to TL_1251 after it reads the name it immediately skips to trying to read in the shift.

this is the error that is generated:

    Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at assignment3.FactoryEmployees.loadEmployeesFromFile(FactoryEmployees.java:244)
at assignment3.FactoryEmployees.<init>(FactoryEmployees.java:43)
at assignment3.FactoryEmployees$1.run(FactoryEmployees.java:403)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

line 244 is

int shift = fileScan.nextInt();

When limiting the loop to only run three times and only printing the shift and up i get

    PW_1234
    James
    Bond
    01/02/10
    1
    10 
    PW_1235
    John
    Brown
    02/03/10
    2
    10.5
    PW_1236
    Howard
    Johnson
    03/04/10
    3

the weird thing is that it ends up printing the hourlyPayRate which i have not coded to print yet

Looks like you have a typo. Try changing case "TM" to case "TL" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM