用Java多形式显示项目

时间:2014-11-20 21:21:36

标签: java polymorphism

我正在尝试从文本文件中读取然后输出到文本文件但我不断收到InputMismatchException错误。我的输入可能有问题,所以有人可以看到我哪里出错了吗?谢谢!

这是我要提取的文件:

S100
Sully
Ross
111-11-1111
8 15 1979
900.00

H205
Joe
Aggie
222-22-2222
3 6 1993
15.25
40

C102
Rev
Elee
333-33-3333
11 22 1985
20000
.065

B115
Johnny
Football
444-44-4444
06 28 1965
12000
.05
400

P206
Miss
Bizbee
555-55-5555
11 06 1977
1.25
1000
X

这是我的代码:

import java.io.*;
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.Calendar;

public class PayrollSystemTest 

{


public static void main(String[] args) throws FileNotFoundException 
{
    File inputFile = new File ("EmployeePayrollInfo.txt");
    Scanner input = new Scanner (inputFile);
    PrintWriter writer = new PrintWriter("Payroll.txt");
    Calendar cal = Calendar.getInstance();
    double currentMonth = cal.get(Calendar.MONTH)+1;
    double birthMonth;
    Employee arrayOfEmployees[] = new Employee[5];
    int count = 0;

    while(input.hasNext()){
        String ID = input.nextLine();

        if(ID.startsWith("S")){

            String first = input.nextLine();
            String last = input.nextLine();
            String ssn = input.nextLine(); //how to hide these
            //Date birth = new Date(input.nextInt(), input.nextInt(), input.nextInt());
            int month = input.nextInt();
            int date = input.nextInt();
            int year = input.nextInt();
            double salary = input.nextInt();
            SalariedEmployee salaryEmployee = new SalariedEmployee(ID, first, last, ssn, month, date, year, salary);
            arrayOfEmployees[count++]= salaryEmployee;
        }
        if(ID.startsWith("H")){

            String first = input.nextLine();
            String last = input.nextLine();
            String ssn = input.nextLine(); //how to hide these
            //Date birth = new Date(input.nextInt(), input.nextInt(), input.nextInt());
            int month = input.nextInt();
            int date = input.nextInt();
            int year = input.nextInt();
            double hours = input.nextInt();
            double wages = input.nextInt();
            HourlyEmployee HourlyEmployee = new HourlyEmployee(ID, first, last, ssn, month, date, year, hours, wages);
            arrayOfEmployees[count++]= HourlyEmployee;
        }
        if(ID.startsWith("C")){

            String first = input.nextLine();
            String last = input.nextLine();
            String ssn = input.nextLine();
            //Date birth = new Date(input.nextInt(), input.nextInt(), input.nextInt());
            int month = input.nextInt();
            int date = input.nextInt();
            int year = input.nextInt();
            double sales = input.nextInt();
            double rate = input.nextInt();
            CommissionEmployee CommissionEmployee = new CommissionEmployee(ID, first, last, ssn, month, date, year, sales, rate);
            arrayOfEmployees[count++]= CommissionEmployee;
        }
        if(ID.startsWith("B")){

            String first = input.nextLine();
            String last = input.nextLine();
            String ssn = input.nextLine();
            //Date birth = new Date(input.nextInt(), input.nextInt(), input.nextInt());
            int month = input.nextInt();
            int date = input.nextInt();
            int year = input.nextInt();
            double sales = input.nextInt();
            double rate = input.nextInt();
            double salary = input.nextInt();
            BasePlusCommissionEmployee BasePlusCommissionEmployee = new BasePlusCommissionEmployee(ID, first, last, ssn, month, date, year, sales, rate, salary);
            arrayOfEmployees[count++]= BasePlusCommissionEmployee;
        }
        if(ID.startsWith("P")){

            String first = input.nextLine();
            String last = input.nextLine();
            String ssn = input.nextLine();
            //Date birth = new Date(input.nextInt(), input.nextInt(), input.nextInt());
            int month = input.nextInt();
            int date = input.nextInt();
            int year = input.nextInt();
            double wages = input.nextInt();
            double pieces = input.nextInt();
            PieceWorker PieceWorker = new PieceWorker(ID, first, last, ssn, month, date, year, wages, pieces);
            arrayOfEmployees[count++]= PieceWorker;

        }
    }
        for (Employee currentEmployee : arrayOfEmployees)
        {
            writer.println( currentEmployee );
            if(currentMonth == currentEmployee.getMonth())
            {
                writer.println("earned" + currentEmployee.earnings() + 100 );
            }else{
                writer.println("earned" + currentEmployee.earnings());
            }



            writer.println("earned" + currentEmployee.earnings());
        }



        for (int j = 0; j < arrayOfEmployees.length; j++)
        {   
            writer.println("Employee is a" + arrayOfEmployees[j].getClass().getName());
        }
        writer.close();
    }


}

2 个答案:

答案 0 :(得分:0)

在执行每个nextLine()之前,您必须检查是否有任何内容要阅读。那就是问题所在。您正在使用nextLine()逐行读取,而不是逐字逐句。使用String.split()

String record = input.nextLine();

String currentRecord[] = record.split(" ");

// currentRecord[0] -> ID
// currentRecord[1] -> first
// currentRecord[2] -> second
// currentRecord[3] -> ssn
// Integer.parseInt(currentRecord[4]) -> month
//      .
//      .
//      .

答案 1 :(得分:0)

您必须始终处理代码中的错误,例如,您只需假设输入始终采用您想要的格式,但有时则不是,例如,如果您的输入开头,则需要读取7行S但它只有5行。您的问题有多种解决方案,但我认为重要的是您了解检查输入的重要性。