Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chapt11;
- import java.io.FileReader;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.lang.Comparable;
- public class CH11AS8App {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- Student [] studentArray;
- System.out.println("Welcome to the Student Scores Application.");
- System.out.println();
- String lastName;
- String firstName;
- int examScore;
- Scanner aScanner = new Scanner(new FileReader(
- "src//chapt11//ch11AS8data.txt"));
- int nStudent = 1;
- studentArray = new Student[nStudent];
- int counter=0;
- while (aScanner.hasNext()) {
- lastName = aScanner.next();
- firstName = aScanner.next();
- examScore = aScanner.nextInt();
- System.out.println("Student " + nStudent++ + " " + firstName
- + " " + lastName + " " + +examScore);
- studentArray[counter] = new Student(lastName, firstName, examScore);
- counter++;
- }
- Arrays.sort(studentArray,0,nStudent);
- System.out.print(studentArray);
- }
- static class Student implements Comparable<Student> {
- private String firstName;
- private String lastName;
- private int examScore;
- public Student(String firstName, String lastName, int examScore) {
- this.firstName = firstName;
- this.examScore = examScore;
- this.lastName = lastName;
- }
- // Get & Set Methods
- public int getExamScore() {
- return examScore;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getLastName() {
- return lastName;
- }
- @Override
- public int compareTo(Student s) {
- if (s.lastName.equals(lastName)) {
- return firstName.compareToIgnoreCase(s.firstName);
- }
- return lastName.compareToIgnoreCase(s.lastName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement