如何从ArrrayList <string>获取整数值(这包含字符串和数字)

时间:2017-01-30 02:52:48

标签: java string arraylist collections

public static int processData(ArrayList<String> array) 
   {
       ArrayList <Integer> no=new  ArrayList<Integer>();
       Iterator it=array.iterator();
        String[] strValues;
 while(it.hasNext())
           {
               strValues = array.toString().split(",");
               System.out.println(it.next());
               strValues = array.toString().split(",");
               no.add(0,strValues[2]);
               no.add(1,strValues[6]);
               strValues = array.toString().split(",");
              }          
        return 1;
    }

    public static void main (String[] args) {
        ArrayList<String> inputData = new ArrayList<String>();
        try {
            Scanner in = new Scanner(new BufferedReader(new FileReader("D:\\dmo\\input.txt")));
            while(in.hasNextLine()) {
                String line = in.nextLine().trim();
                if (!line.isEmpty()) // Ignore blank lines
                    inputData.add(line);
            }
            int retVal = processData(inputData);
            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("D:\\dmo\\output.txt")));
            output.println("" + retVal);
            output.close();
        } catch (IOException e) {
            System.out.println("IO error in input.txt or output.txt");
        }
    }
}

输入文本文件包含记录 20,AB CD,55,876000 22,John Carter,57,987520 23,abrahim,55,5420130 24,mariya,54,6952403 25,塞丽娜,57,5895421

此数据作为Arraylist数组传递给processData()函数。我想计算相同部门ID的平均工资。例如。平均55 de

2 个答案:

答案 0 :(得分:0)

在每一行上创建一个人物对象(假设每一行都有一个人物数据)并将它们添加到一个列表中。在计算每个部门的工资平均值时,首先筛选出该部门的人员,然后计算总工资,然后除以该部门的人员。希望它会有所帮助。

public class Person {
    private int id;
    private String name;
    private int deptId;
    private double salary;

    public Person() {
    }

    public Person(int id, String name, int deptId, double salary) {
        this.id = id;
        this.name = name;
        this.deptId = deptId;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDeptId() {
        return deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", deptId=" + deptId +
                ", salary=" + salary +
                '}';
    }
}

public class Main {
    /*public static int processData(ArrayList<String> array) {
        ArrayList<Integer> no = new ArrayList<Integer>();
        Iterator it = array.iterator();
        String[] strValues;
        while (it.hasNext()) {
            strValues = array.toString().split(",");
            System.out.println(it.next());
            strValues = array.toString().split(",");
            no.add(0, strValues[2]);
            no.add(1, strValues[6]);
            strValues = array.toString().split(",");
        }
        return 1;
    }*/

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        try {
            Scanner in = new Scanner(new BufferedReader(new FileReader("input.txt")));
            while (in.hasNextLine()) {
                String line = in.nextLine().trim();
                if (!line.isEmpty()) // Ignore blank lines
                    personList.add(getPerson(line));
            }
//            int retVal = processData(inputData);
//            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
//            output.println("" + retVal);
//            output.close();

            double avgSalary = getAvgSalary(personList, 55);
            System.out.println(avgSalary);

        } catch (IOException e) {
            System.out.println("IO error in input.txt or output.txt");
            e.printStackTrace();
        }
    }

    private static Person getPerson(String str) {
        String[] strings = str.split(",");
        Person person = new Person(Integer.valueOf(strings[0].trim()), strings[1].trim(), Integer.valueOf(strings[2].trim()), Double.valueOf(strings[3].trim()));
        System.out.println(person);
        return person;
    }

    private static double getAvgSalary(List<Person> personList, int deptId) {
        List<Person> deptPersonList = new ArrayList<>();
        for (Person person : personList) {
            if (person.getDeptId() == deptId) {
                deptPersonList.add(person);
            }
        }

        double totalSalary = 0;
        for (Person person : personList) {
            totalSalary += person.getSalary();
        }

        return totalSalary / deptPersonList.size();
    }
}

答案 1 :(得分:0)

create String str_seperate =&#34;&#34 ;;使用常规exp迭代数组。 do str = array.remove(index);并使用Scanner sc = new Scanner(str).useDemiliter(&#34; \ s *,\ W \ s *&#34;)查找int和字符串数据..