fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. /* DO NOT CHANGE ANYTHING ABOVE THIS LINE */
  7. /* You may add any imports here, if you wish, but only from the
  8.   standard library */
  9.  
  10. /* Do not add a namespace declaration */
  11. public class Main {
  12. public static Map<String,Integer> processData(ArrayList<String> array) {
  13. Map<String,Integer> retVal = new HashMap<String,Integer>(); //Result Map
  14. Map<String,Integer> map = new HashMap<String,Integer>(); //Intermediate Map To Store Subjects and Marks
  15. for(int i=0;i<array.size();++i)
  16. {
  17. String input = array.get(i);
  18. System.out.println(input);
  19. boolean flag1 = false, flag2 = false ;
  20. String id="" , subject="", marks="";
  21. for(int j=0;j<input.length();++j) //Parsing The Input
  22. {
  23. if(flag2){
  24. marks= marks+input.charAt(j);
  25. continue;
  26. }
  27. if(!flag1){
  28. if(input.charAt(j)=='|')
  29. flag1 = true;
  30. else
  31. id=id+input.charAt(j);
  32.  
  33. }
  34. else if(flag1){
  35. if(input.charAt(j)=='|')
  36. flag2 = true;
  37. else
  38. subject=subject+input.charAt(j);
  39. }
  40.  
  41.  
  42.  
  43. }
  44. int Id = Integer.parseInt(id);
  45. int Marks = Integer.parseInt(marks);
  46. if(map.get(subject)==null){
  47. map.put(subject,new Integer(Marks));
  48. retVal.put(subject,new Integer(id));
  49. }
  50. else{
  51. int m = map.get(subject);
  52. if(Marks>m){
  53. map.put(subject,new Integer(Marks));
  54. retVal.put(subject,new Integer(id));
  55. }
  56.  
  57. }
  58.  
  59. }
  60.  
  61. return retVal;
  62. }
  63.  
  64. public static void main (String[] args) {
  65.  
  66. ArrayList<String> inputData = new ArrayList<String>();
  67. String line;
  68. try {
  69. Scanner in = new Scanner(new BufferedReader(new FileReader("input.txt")));
  70. while(in.hasNextLine())
  71. inputData.add(in.nextLine());
  72. Map<String,Integer> retVal = processData(inputData);
  73. PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
  74. for(Map.Entry<String,Integer> e: retVal.entrySet())
  75. output.println(e.getKey() + ": " + e.getValue());
  76. output.close();
  77. } catch (IOException e) {
  78. System.out.println("IO error in input.txt or output.txt");
  79. }
  80.  
  81.  
  82. }
  83. }
Success #stdin #stdout 0.1s 48536KB
stdin
Standard input is empty
stdout
IO error in input.txt or output.txt