Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class StudentTester{
  8. public static void main(String[] args)throws Exception{
  9. Scanner input = new Scanner(new java.io.File(args[args.length-1]));
  10. ArrayList<Student> list = new ArrayList<Student>();
  11.  
  12.  
  13. while(input.hasNext())
  14. {
  15. String fname;
  16. double gpa;
  17.  
  18. fname = input.next();
  19. gpa=Double.parseDouble(input.next());
  20.  
  21. Student stu1 = new Student(fname, gpa);
  22.  
  23. list.add(stu1);
  24. }
  25.  
  26. Collections.sort(list, new Comparator<Student>(){
  27.  
  28. public int compare(Student A, Student B){
  29. return A.getFirstName().compareTo(B.getFirstName());
  30. }
  31. });
  32. System.out.println("Students names listed in alphabetic order: ");
  33.  
  34. for(Student stu1: list){
  35. System.out.println(stu1.getFirstName() + " " + stu1.getGpa());
  36. }
  37.  
  38. Collections.sort(list, new Comparator<Student>(){
  39. public int compare(Student A, Student B){
  40. return Double.valueOf(A.getGpa()).compareTo(Double.valueOf(B.getGpa()));
  41. }
  42. });
  43. System.out.println(" ");
  44.  
  45. //reverses the list, decending order for gpa
  46. Collections.reverse(list);
  47.  
  48. System.out.println("Students gpa in decending order: ");
  49.  
  50. for(Student stu1: list){
  51. System.out.println(stu1.getFirstName() + " " + stu1.getGpa());
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement