我无法获得员工的平均年龄

时间:2016-08-29 10:05:43

标签: java

This is the problem

我的源代码如下: 我使用文件阅读器方法从记事本中保存的员工列表中获取所有文件。我的代码没有语法错误,但出于某种原因,不使用年龄和工资变量,平均年龄不是从员工列表计算的,而是从我初始化年龄的值计算出来的; 0。有什么建议?

public EmployeeAverage() {
    Container contentPane;

    File file;
    FileReader fileReader = null;
    BufferedReader buffReader;

    file = new File("C:\\Users\\user\\Downloads\\Codefile\\code\\employees.txt");
    try {
        fileReader = new FileReader(file);
    } catch (FileNotFoundException e) {
        System.out.println("Cannot open employees file");
        System.exit(1);
    }
    buffReader = new BufferedReader(fileReader);

    ArrayList employees = new ArrayList();
    String line = "";
    String first;
    String last;
    String SSN;
    int age=0;
    double salary=0;
    int lineCount = 0;

    while (true) {
        try {
        line = buffReader.readLine();
        if (line == null)
            break;
        lineCount++;
        first = line;
        line = buffReader.readLine(); lineCount++;
        last = line;
        line = buffReader.readLine(); lineCount++;
        SSN = line;
        line = buffReader.readLine(); lineCount++;
        line=Integer.toString(age);
        line = buffReader.readLine(); lineCount++;
        line=Double.toString(salary);
        } catch (IOException e) {
        System.err.println("Data file error at line "+lineCount);
        System.err.println("Ignoring rest of file");
        break;
        } catch (NumberFormatException e) {
        System.err.println("Illegal age or salary at line "+lineCount);
        System.err.println("Ignoring rest of file");
        break;
        }
        employees.add(new Employee(first, last, SSN, age, salary));
    }
    System.out.println("Number of employees: " + employees.size());

    Collections.sort(employees);

    // when we get here, employees refers to a sorted employee list

    setSize(300,500);
    setTitle("Employee Averages");
    //setLocation(150,250);

    contentPane = getContentPane();
    contentPane.setBackground(Color.white);
    contentPane.setLayout(new BorderLayout());

    // Create a panel for the employee list
    JPanel employeeList = new JPanel();
    employeeList.setLayout(new BorderLayout());
    employeeList.setBorder(BorderFactory.createTitledBorder("Employees"));

    // Create the employee list, make it into a scroll pane, and
    // add it to the employee list panel
    list = new JList(employees.toArray());
    JScrollPane employeeChooser = new JScrollPane(list);
    employeeList.add(employeeChooser, BorderLayout.CENTER);

    // Create the radio buttons
    // Ignore this until you finish Checkpoint 2.

    // Create a text area to show the average
    avg = new JTextArea("");

    // Now put both the employee list and the average
    // into the top-level pane
    contentPane.add(employeeList, BorderLayout.CENTER);
    contentPane.add(avg,BorderLayout.SOUTH);

    // Monitor changes in the employee list selection
    list.addListSelectionListener(this);

    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void valueChanged(ListSelectionEvent e) {
    displayAverage();
    }

    public void displayAverage() {

    if (list == null)
        return; // don't do anything if there is no list yet

    Object [] selectedEmployees = list.getSelectedValues();
    int len = selectedEmployees.length;
    if (len == 0) {
        avg.setText("");
        return;
    }

    DecimalFormat fmt = new DecimalFormat("#0.0");

    int sum = 0;
    for (int i=0 ; i<len ; i++) {
        Employee emp = (Employee)selectedEmployees[i];
        sum += emp.getAge();
    }
    avg.setText("Average age = " + fmt.format((double)sum/len));
    }
}

This is the employees list used by the file reader method

1 个答案:

答案 0 :(得分:2)

您希望这些行能做什么?

line=Integer.toString(age);
line=Double.toString(salary);

看起来你得到了它的背叛。您应该从输入行解析int agedouble salary

line = buffReader.readLine(); lineCount++;
age=Integer.parseInt(line);
line = buffReader.readLine(); lineCount++;
salary=Double.parseDouble(line);