将文本文件调用到java

时间:2016-11-02 22:29:01

标签: java arraylist text-files extends

当我运行以下代码时,它只打印“year \ tposition \ tname \ tMonthlySalary \ tAnnualSales \ tStockPrice”,而不是存储在data.txt中的信息。它也不计算2014年的平均值和2015年的平均值。我使用NetBeans来获取此代码,但没有看到任何错误。

data.txt有

2014员工史密斯,詹姆斯2000

2014年推销员Johnson,Mary 3500 10000

2014年行政威廉姆斯,罗伯特2800 25

2015员工琼斯,Barbara 4000

2015年推销员布朗,William 3700 5000

2015年执行戴维斯,詹妮弗2600 51

2014员工Miller,Richard 3900

2014年推销员Wilson,Susan 4200 7000

2014年执行摩尔,Joseph 1900 65

2015年员工Taylor,Dorothy 3700

2015年推销员安德森,克里斯托弗3200 15000

2015执行托马斯,南希4400 33

class Employee{
private String year;
private String name;
private String position;
private double monthlySalary;
public Employee(String year, String position, String name, double monthlySalary){
    this.year = year;
    this.position = position;
    this.name = name;
    this.monthlySalary = monthlySalary;
}
public double annualSalary(){
    return monthlySalary*12;
}
public String toString(){
    return year+"\t"+position+"\t"+name +"\t" + monthlySalary +"\t";
}
}class Salesman extends Employee{
private double annualSales;

public Salesman(String year, String position, String name, double monthlySalary, double annualSales){
    super(year, position, name, monthlySalary);
    this.annualSales = annualSales;
}

public double annualSalary(){
    double commission = this.annualSales*0.2;
    if(commission>20000){
        commission = 20000;
    }
    return super.annualSalary()+commission;
}
public String toString(){
    return super.toString()+annualSalary();
}
}class Executive extends Employee{
private double stockPrice;
public Executive(String year, String position, String name, double monthlySalary, double stockPrice){
    super(year, position, name, monthlySalary);
    this.stockPrice = stockPrice;
}
public double annualSalary(){
    double bonus=0;
    if(this.stockPrice>50){
        bonus = 30000;
    }
    return super.annualSalary() + annualSalary();
}
public String toString(){
    return super.toString()+"\t"+ this.stockPrice+"\n";
}

}

class TestEmployee {
ArrayList<Employee> employee2014 = new ArrayList<Employee>();
ArrayList<Employee> employee2015 = new ArrayList<Employee>();

public void MakeArray(){
    try{
        Scanner sc = new Scanner(new File("data.txt"));
        while(sc.hasNext()){
            String year = sc.nextLine();
            String[] data = year.split(" ");
            if(data[0] == "2014"){
                if(data[1].equalsIgnoreCase("Employee")){
                    employee2014.add(new Employee(data[0],data[1],data[2],Double.parseDouble(data[3])));
                }else if(data[1].equalsIgnoreCase("Salesman")){
                    employee2014.add(new Salesman(data[0],data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));                   
                }else if(data[1].equalsIgnoreCase("Executive")){
                    employee2014.add(new Executive(data[0],data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));                    
                }
            }if(data[0] == "2015"){
                if(data[1].equalsIgnoreCase("Employee")){
                    employee2015.add(new Employee("2015",data[1],data[2],Double.parseDouble(data[3])));
                }else if(data[1].equalsIgnoreCase("Salesman")){
                    employee2015.add(new Salesman("2015",data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));                   
                }else if(data[1].equalsIgnoreCase("Executive")){
                    employee2015.add(new Executive("2015",data[1],data[2],Double.parseDouble(data[3]),Double.parseDouble(data[4])));                    
                }
            }
        }    
    }catch(FileNotFoundException nf){
        System.out.println("File not found");
    }
}public void Result(){
    double average2014=0;
    double total2014=0;
    double average2015=0;
    double total2015=0;
    System.out.print("Data of 2014:\n"
            + "Year\tPosition\tName\tMonthlySalary\tAnnualSales\tStockPrice");
    for(Employee employee14: employee2014){
        System.out.println(employee14.toString());
        total2014 +=employee14.annualSalary();
    }average2014 = total2014/employee2014.size();
    System.out.print("\nThe average annual salary of 2014 is " + average2014+"\n");
    System.out.println("==================================");

    System.out.print("Data of 2015:\n"
            + "Year\tPosition\tName\tMonthlySalary\tAnnualSales\tStockPrice");
    for(Employee employee15: employee2015){
        System.out.println(employee15.toString());
        total2015 +=employee15.annualSalary();
    }average2015 = total2014/employee2015.size();
    System.out.print("\nThe average annual salary of 2015 is " + average2015+"\n");
}
public static void main(String[] args){
    TestEmployee te = new TestEmployee();
    te.MakeArray();
    te.Result();  
}  

}

1 个答案:

答案 0 :(得分:0)

在“MakeArray”方法中(方法的第一个字母应该是小写的约定)你应该使用equals方法而不是“==”运算符:

if(data[0] == "2014") --> if(data[0].equals("2014"))
if(data[0] == "2015") --> if(data[0].equals("2015"))

还修复了annualSalary()(在Executive类中)方法如下:

public double annualSalary(){
    double bonus=0;
    if(this.stockPrice>50){
        bonus = 30000;
    }
    return super.annualSalary() + bonus;
}