fork download
  1. /* Save this in a file called Main.java to compile and test it */
  2.  
  3. /* Do not add a package declaration */
  4. import java.util.*;
  5. import java.io.*;
  6.  
  7. /* You may add any imports here, if you wish, but only from the
  8.   standard library */
  9.  
  10. public class Main {
  11. public static int processData(ArrayList<String> array) {
  12.  
  13. String str[];
  14. int low=100,low1,count=0;
  15. for(String s : array){
  16. str= s.split(",");
  17. low1=Integer.parseInt(str[2].trim());
  18. if(low>low1){
  19. count++;
  20. low=low1;
  21. }
  22. }
  23. int sal=0,tot=0;
  24. int avg=0;
  25. for(String s : array){
  26. str=s.split(",");
  27. int dept=Integer.parseInt(str[2].trim());
  28. sal=Integer.parseInt(str[3].trim());
  29. if(low==dept) {
  30. tot=tot+sal;
  31. }
  32. }
  33.  
  34. avg=tot/count;
  35.  
  36. return avg;
  37. }
  38.  
  39. public static void main (String[] args) {
  40. ArrayList<String> inputData = new ArrayList<String>();
  41. try {
  42. Scanner in = new Scanner(new BufferedReader(new FileReader("input.txt")));
  43. while(in.hasNextLine()) {
  44. String line = in.nextLine().trim();
  45. if (!line.isEmpty()) // Ignore blank lines
  46. inputData.add(line);
  47. }
  48. int retVal = processData(inputData);
  49. PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
  50. output.println("" + retVal);
  51. output.close();
  52. } catch (IOException e) {
  53. System.out.println("IO error in input.txt or output.txt");
  54. }
  55. }
  56. }
Success #stdin #stdout 0.08s 34052KB
stdin
22, Rajan Anand, 11, 1600000
23, Swati Patil, 12, 800000
27, Vijay Chawda, 11, 800000
29, Basant Mahapatra, 11, 600000
32, Ajay Patel, 12, 350000
34, Swaraj Birla, 12, 350000
stdout
IO error in input.txt or output.txt