-1

Hello I am solving this problem given to me where I have to find average salary of a person which has least index id

import java.util.*;
import java.io.*;

public class Main {
    public static int processData(ArrayList<String> array) {
      for (String elem :array){
       System.out.println(elem);
      }

        return 0;
    }

    public static void main (String[] args) {
        ArrayList<String> inputData = new ArrayList<String>();
        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
                    inputData.add(line);
            }
            int retVal = processData(inputData);
            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
            output.println("" + retVal);
            output.close();
        } catch (IOException e) {
            System.out.println("IO error in input.txt or output.txt");
        }
    }
}

the program is accepting input from text file as follows

282, ij, 11, 1600000
273, cbg, 12, 800000
567, nj, 11, 800000
539, cb, 11, 600000

So the output will be

11 520000

I am able to print the elements from array list but not been able to access particular elements. Can anyone help me to access particular element which is, in this case, 11,160000 and so on?

Thank you in advance

hmatar
  • 2,437
  • 2
  • 17
  • 27
  • 1
    Have you looked at `.split()` or `StringTokenizer` ? – Anand Undavia Mar 02 '17 at 07:14
  • Possible duplicate of [Java: CSV File Easy Read/Write](http://stackoverflow.com/questions/14226830/java-csv-file-easy-read-write) – Jiri Tousek Mar 02 '17 at 07:18
  • 1
    First of all, you should learn how to divide your problems into smaller problems. After that solve each problem separately and bring them together. If you have any problem with a particular smaller problem it is easier to solve that. It is not about coding, but way of dealing with problems. – halil Mar 02 '17 at 07:28

3 Answers3

0

Hint

You can calculate the AVG of the index 11 like this :

public static int processData(ArrayList<String> array) {
    String[] spl;
    int avg = 0, nbr = 0;

    for (String elem : array) {
        //split your String with ", " and space
        spl = elem.split(", ");
        //the index exist in the 3ed position, so you have to check your index if 11 then you can get its value
        if(spl[2].equals("11")){
            avg+=Integer.parseInt(spl[3]);
            nbr++;
        }
        System.out.println(elem);
    }
    System.out.println((avg/nbr));
    return avg / nbr;
}

When you print in your code you have to use :

output.println("11 " + retVal);

Hope this can gives you an idea.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

You create a List of String to store employee data that contains multiple fields.
You should not as it mixes data employees.

The general idea I propose you :

1) Instead of storing all information in a List of String, use a List of Employee.

replace

ArrayList<String> inputData = new ArrayList<String>();

by

List<Employee> employees = new ArrayList<Employee>();

2)Use each read line that represents a person to create a instance of a custom Object, for example Employee.

So replace

 inputData.add(line);

by something like that

String[] token = line.split(",");
Employee employee= new Employee(Integer.valueOf(token[0]),token[1],Integer.valueOf(token[2]),Integer.valueOftoken[3]));
employees.add(employee);

3) During this iteration to read the file, you can store in a variable that is the Employee with the minimum id.

4) After reading the file, you know the Employee with the minimum id. So you can iterate on the List of Employee and sum the salaries of the Employee that has this id and count the number of salary for this Employee.
When the loop is finished compute the avg : float avg = sum / (float)count;

It is not the most optimized way but it makes the job.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

The following code will do what you need.

public class MyMain {

    private static String inputFilePath = "/path/to/input.txt";
    private static String outputFilePath = "/path/to/output.txt";

    public static int processData(ArrayList<MyData> array) {
        if (array.size() > 0) {
            int minId = array.get(0).getData3();

            for (MyData elem : array) {
                if(elem.getData3() < minId) {
                    minId = elem.getData3();
                }
            }

            int count = 0;
            int total = 0;
            for (MyData myData : array) {
                if(myData.getData3() == minId) {
                    count++;
                    total += myData.getData4();
                }
            }

            System.out.println("Min ID : " + minId + " -- Avg Sal : " + total/count);
        }

        return 0;
    }

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

}

class MyData {
    int data1;
    String data2;
    int data3;
    int data4;

    public MyData() {
        // TODO Auto-generated constructor stub
    }

    public MyData(String data) {
        String dataArr[] = data.split(",");
        this.data1 = Integer.parseInt(dataArr[0].trim());
        this.data2 = dataArr[1].trim();
        this.data3 = Integer.parseInt(dataArr[2].trim());
        this.data4 = Integer.parseInt(dataArr[3].trim());
    }

    public int getData1() {
        return data1;
    }

    public void setData1(int data1) {
        this.data1 = data1;
    }

    public String getData2() {
        return data2;
    }

    public void setData2(String data2) {
        this.data2 = data2;
    }

    public int getData3() {
        return data3;
    }

    public void setData3(int data3) {
        this.data3 = data3;
    }

    public int getData4() {
        return data4;
    }

    public void setData4(int data4) {
        this.data4 = data4;
    }

}
Avinash
  • 4,115
  • 2
  • 22
  • 41