简体   繁体   中英

How to pick certain objects from a serialized arraylist?

In option two and three I have no clue how to reference the serialized arraylist for certain data types(ie the string last name or the double gpa) and then compare them. On the lab sheet we got instructions on how to do everything else, but for doing options 2 and 3 it literally said "figure it out", the professor has never been good at explaining things to the class. So I'm hoping someone can explain it to me.

Edit: 901 number is what student ID numbers are called at my university

Here's my Student class(everything should be fine in it)

import java.io.Serializable;
public class Student implements Serializable
{
    private String lastName, firstName;
    private double gpa;
    private int studentID, gradYear;
    public Student(int studentID, String lastName, String firstName, double gpa, int gradYear)
    {
        this.studentID = studentID;
        this.lastName = lastName;
        this.firstName = firstName;
        this.gpa = gpa;
        this.gradYear = gradYear;
    }
    public int getStudentID()
    {
        return studentID;
    }
    public String getLastName()
    {
      return lastName;
    }
    public String getFirstName()
    {
      return firstName;
    }
    public double getGPA()
    {
        return gpa;
    }
    public int getGradYear()
    {
        return gradYear;
    }
    public String toString()
    {
        return "Student ID: " + studentID + " Name: " + lastName.trim() + ", " + firstName.trim() + " GPA: " + gpa + " Grad year: " + gradYear;
    }
}

Here is the test class where I need help(outside of options 2 & 3 I think it all works)

import java.util.*;
import java.io.*;
public class StudentTestOS
{
    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
        boolean done = false;
        ArrayList<Student> sList = new ArrayList<Student>();
        File sFile = new File("studentOS.dat");
        if(sFile.exists())
        {
            FileInputStream myFIS = new FileInputStream(sFile);
            ObjectInputStream sIn = new ObjectInputStream(myFIS);
            sList = (ArrayList<Student>)sIn.readObject();
            sIn.close();
        }    
        System.out.println("Students on file: ");
        for(int i = 0; i < sList.size(); i++)
            System.out.println(sList.get(i).toString());
        do
        {
        Scanner myScanner = new Scanner(System.in);        
        while (!done)
        {
            System.out.println("1 - add a student");
            System.out.println("2 - display student info");
            System.out.println("3 - display student info given their last name");
            System.out.println("4 - exit");
            int choice = Integer.parseInt(myScanner.nextLine());
            if (choice == 1)
            {
                System.out.print("Enter 901 number: ");
                int studentID = Integer.parseInt(myScanner.nextLine());
                System.out.print("Enter last name: ");
                String lastName = myScanner.nextLine();
                System.out.print("Enter first name: ");
                String firstName = myScanner.nextLine();
                System.out.print("Enter gpa: ");
                double gpa = Double.parseDouble(myScanner.nextLine());
                System.out.print("Enter grad year: ");
                int gradYear = Integer.parseInt(myScanner.nextLine());
                Student myStudent = new Student(studentID, lastName, firstName, gpa, gradYear);
                sList.add(myStudent);
            }
            else if (choice == 2)
            {
               System.out.print("Enter 901 number: ");
               int studentID = Integer.parseInt(myScanner.nextLine());
            }
            else if (choice == 3)
            {
                System.out.println("Enter last name: ");
                String lastName = myScanner.nextLine();
            }
            else if (choice == 4)
            {
               done = true;
            }
            else
                System.out.println("Invalid menu choice!");
       }
       System.out.println("Goodbye!");
    }while(!done);
    FileOutputStream myFOS = new FileOutputStream(sFile);
    ObjectOutputStream sOut = new ObjectOutputStream(myFOS);
    sOut.writeObject(sList);
    sOut.close();
}
}

You can have a loop and compare the objects one by one on the given attributes( Id for choice2 and lastName for choice 3).

For choice 2 : -

System.out.print("Enter 901 number: ");
int studentID = Integer.parseInt(myScanner.nextLine());
for(Student student : sList){
   if(student.getStudentID == studentId){
     System.out.println(student); 
     break; // As student Id is unique.So, once we found the student no need to loop further.
   }
}

For choice 3, compare it on lastName and do not put break statement as there can be many with same lastName.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM