Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.text.*;
- import java.util.*;
- class Student
- {
- String name;
- int id;
- float gpa;
- } // Student
- public class z
- {
- static void printStudents(Student[] student, int nStudents)
- {
- String b = " ";
- int i;
- for (i = 0; i < nStudents; i++)
- {
- System.out.print("Name = " + student[i].name);
- System.out.print(b.substring(student[i].name.length()));
- System.out.print(" ID = "
- + new DecimalFormat("0000000").format(student[i].id));
- System.out.println(" gpa = " + student[i].gpa);
- } // for
- } // printStudents
- public static void main(String[] argv) throws Exception
- {
- // open a file for input
- BufferedReader fin;
- BufferedReader cin;
- cin = new BufferedReader(new InputStreamReader(System.in));
- // create an empty list of capacity 100
- int nStudents = 0;
- Student[] student = new Student[100];
- // read and save the records
- int aScore; // loop counter
- for (i = 0; i < student.length; i++)
- {
- System.out.print("Please enter score[" + i + "]: ");
- student[i] = new Double(cin.readLine()).intValue();
- } // for
- while (true)
- {
- // create a record and read it from file
- Student aStudent = new Student();
- String name;
- System.out.print("What is your name? [quit to exit] ");
- name = cin.readLine();
- aStudent.name = name;
- int id;
- System.out.print("What is your id? ");
- id = new Double(cin.readLine()).intValue();
- aStudent.id = id;
- float gpa;
- System.out.print("What is your grade point average? ");
- gpa = new Double(cin.readLine()).floatValue();
- aStudent.gpa = gpa;
- // add record to list, if it's not full
- if (nStudents < student.length)
- student[nStudents++] = aStudent;
- } // while
- // sort the students by name
- for (int i = 0; i < nStudents; i++)
- {
- for (int j = i + 1; j < nStudents; j++)
- {
- if (student[i].gpa < student[j].gpa) // then swap
- {
- Student temp = student[i];
- student[i] = student[j];
- student[j] = temp;
- }
- }
- }
- printStudents(student, nStudents);
- } // main
- } // public class
Add Comment
Please, Sign In to add comment