从ArrayList中提取数字

时间:2013-10-14 22:33:26

标签: java url arraylist

我试图从一个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比较陌生。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

计算平均值

答案 2 :(得分: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;
        }
    }

我很确定必须采用一些更清洁的方式来做到这一点。轮空!