【问题标题】:How to get integer value from ArrrayList<String> (this contains string as well as numbers)如何从 ArrayList<String> 获取整数值(这包含字符串和数字)
【发布时间】:2017-06-15 04:03:00
【问题描述】:
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,约翰卡特,57,987520 23,亚伯拉罕,55,5420130 24,玛丽亚,55,8952403 25,瑟琳娜,57,7895421

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

【问题讨论】:

  • 987520 23 代码还是数字?它之间有一个空格。
  • 输入文本包含 20, AB CD ,55 ,876000 22, John Carter, 57,987520 23, abrahim ,55,5420130 24,mariya , 55,8952403 25,serena , 57,7895421 first no是e-id,2是姓名,3rd是depart-id,4是salary
  • @LKTN.25 它的号码,它是 22,John Carter,57,987520,

标签: java string arraylist collections


【解决方案1】:

创建字符串 str_seperate="";使用常规 exp 迭代数组。做 str=array.remove(index);并使用 Scanner sc=new Scanner(str).useDemiliter("\s*,\W\s*") 找出 int 和 string 数据..

【讨论】:

    【解决方案2】:

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

    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();
        }
    }
    

    【讨论】:

    • 将 input.txt 文件位置更改为您的。我没有得到 output.txt 的使用,所以把它注释掉了。希望它会有所帮助。
    • 不...我必须阅读该文本文件。而且我只允许更改 processData.. int processData(ArrayList array) retuns string.. 例如20, AB CD ,55 ,876000, 22, John Carter, 57,987520 ,23, abrahim ,55,5420130 ,24,mariya , 55,8952403, 25,serena , 57,7895421... 然后如何提取 55 , 876000 ,57,987520 等从字符串计算输入的最低dep-td的平均值...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2016-08-26
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多