Advertisement
damoncard

Programming Exam 1st

Mar 27th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. // Class StudentLog
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4. import java.io.File;
  5. import java.io.PrintWriter;
  6.  
  7. class StudentLog {
  8.  
  9.   public static void printStudent(String n, int id)
  10.   {
  11.    System.out.println(id+ " " + n);
  12.   }
  13.  
  14.   public static StudentInfo[] sortedList(ArrayList<StudentInfo> a)
  15.   {
  16.    StudentInfo[] ob = new StudentInfo[a.size()];
  17.     for (int q = 0 ; q < a.size() ; q++)
  18.     {
  19.      ob[q] = a.get(q);
  20.     }
  21.     // sorted
  22.    for (int q = 0 ; q < ob.length ; q++)
  23.    {
  24.     for (int w = q ; w < ob.length ; w++)
  25.     {
  26.      if (ob[q].id < ob[w].id)
  27.      {
  28.       StudentInfo f = ob[q];
  29.       ob[q] = ob[w];
  30.       ob[w] = f;
  31.      }
  32.     }
  33.    }
  34.    return ob;
  35.   }
  36.  
  37.  
  38.  
  39.   public static void main (String [] args) throws Exception{
  40.     // Read
  41.     File f = new File("student_log.txt");
  42.     Scanner s = new Scanner(f);
  43.     ArrayList<StudentInfo> as = new ArrayList<StudentInfo>();
  44.     while(s.hasNextLine())
  45.     {
  46.      int id = s.nextInt();
  47.      String name = s.nextLine();
  48.      StudentInfo si = new StudentInfo(name, id);
  49.      as.add(si);
  50.     }
  51.     s.close();
  52.     // Sorted
  53.     StudentInfo [] ob = sortedList(as);
  54.     for (StudentInfo studentInfo: ob)
  55.     {
  56.      printStudent(studentInfo.name, studentInfo.id);
  57.     }
  58.     // Write
  59.     File nf = new File("student_log_sorted.txt");
  60.     PrintWriter pw = new PrintWriter(nf);
  61.     for (StudentInfo studentINFO : ob)
  62.     {
  63.      pw.print(studentINFO.id + " ");
  64.      pw.println(studentINFO.name);
  65.     }
  66.     pw.close();
  67.   }
  68. }
  69.  
  70. // Class StudentInfo
  71. // Keep ID-name of one member (student)
  72. class StudentInfo {
  73.   public String name;
  74.   public int id;
  75.  
  76.   public StudentInfo(String n, int id)
  77.   {
  78.    this.name = n;
  79.    this.id = id;
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement