0

我试图从 URL 的文本文件中找出一些教授的总薪水和平均薪水。我可以读取数据并根据该行是否包含教授的等级将其分配给 ArrayList,但我不知道如何将每个等级的工资拉出该数字加起来然后平均. 我知道一旦我得到数字,我可能应该将它解析为双精度数,但我不知道如何提取数字。我最初的目标是让文件中的每一行都是一个单独的对象,但我不知道是怎么做到的,所以我就这样做了。我的代码如下:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class University {

private ArrayList<String> all = new ArrayList<>();
private ArrayList<String> associate = new ArrayList<>();
private ArrayList<String> assistant = new ArrayList<>();
private ArrayList<String> full = new ArrayList<>();

public static void main(String[] args) throws MalformedURLException {
    University u = new University();

    }

private void read() throws MalformedURLException{
    URL data = new URL ("http://cs.armstrong.edu/liang/data/Salary.txt");
    try{
        Scanner scan = new Scanner(data.openStream());
        while(scan.hasNextLine()){
            String line = scan.nextLine();
            line.split(" ");
            all.add(line);
            if(line.contains("associate"))
                associate.add(line);
            else if(line.contains("assistant"))
                assistant.add(line);
            else if(line.contains("full"))
                full.add(line);
        }
        scan.close();
    } catch(IOException e){
        e.getStackTrace();
    }
}

private double totalSalary(ArrayList<String> rank){
    //Rank = name of the ArrayList you want
}

}

另外,在 totalSalary 方法的最后,我这样做是否正确?我以前使用过 ArrayLists,但我以前从未对它们做过类似的事情,因为我对 Java 还比较陌生。

4

3 回答 3

0

也许这有帮助:

    public class Test {

        public class Teacher {

            String firstName;
            String lastName;
            String rank;
            Double salary;

            public Teacher(String firstName, String lastName, String rank,
                    Double salary) {
                super();
                this.firstName = firstName;
                this.lastName = lastName;
                this.rank = rank;
                this.salary = salary;
            }

        }

        ArrayList<Teacher> result = new ArrayList<Teacher>();

        public static void main(String[] args) throws MalformedURLException {
            Test u = new Test();
            u.read();
            u.printAverages();
        }

        private void printAverages() {
            Iterator<Teacher> iterator = result.iterator();
            double assistantHolder = 0, assistantCounter = 0, 
                   assosiatedHolder = 0, assosiatedCounter = 0,
                   fullHolder = 0, fullCounter = 0;
            while (iterator.hasNext()) {
                Teacher teacher = iterator.next();
                if (teacher.rank.equals("associate")) {
                    assosiatedHolder += teacher.salary;
                    assosiatedCounter++;
                } else if (teacher.rank.equals("assistant")) {
                    assistantHolder += teacher.salary;
                    assistantCounter++;
                } else if (teacher.rank.equals("full")) {
                    fullHolder += teacher.salary;
                    fullCounter++;
                }
            }
            System.out.println("Associate average:"+(assosiatedHolder/assosiatedCounter));
            System.out.println("Assistant average:"+(assistantHolder/assistantCounter));
            System.out.println("Full average:"+(fullHolder/fullCounter));
        }

        private ArrayList<Teacher> read() throws MalformedURLException {
            URL data = new URL("http://cs.armstrong.edu/liang/data/Salary.txt");
            try {
                Scanner scan = new Scanner(data.openStream());
                while (scan.hasNext()) {
                    String line = scan.nextLine();
                    String[] tokens = line.split("\\s+",4);
                    String firstName = tokens[0], lastName = tokens[1], rank = tokens[2];
                    Double salary = Double.valueOf(tokens[3]);
                    result.add(new Teacher(firstName, lastName, rank, salary));
                }
                scan.close();
                return result;
            } catch (IOException e) {
                e.getStackTrace();
            }
            return result;
        }
    }

我很确定这一定是一种更清洁的方法。再见!

于 2013-10-14T23:15:47.920 回答
0

要获取值,请调用ArrayList.get()。对每个元素(在循环中)执行此操作,并跟踪循环外变量中的总和。

于 2013-10-14T22:36:31.173 回答
0

您可以简单地使用三个 x 2 = 六个类变量来定义您拥有的三个级别(全职、助理和助理)的薪水的总和和平均值。您的 if 逻辑仍然可以,只要 if 条件匹配,就将相应的薪水添加到特定类变量的总数中。完成后,您可以通过 sum/array_list.size(); 计算平均值。

于 2013-10-14T22:43:41.783 回答