有没有办法以不同的方式访问添加到 ArrayList 的日期以更改其数据?

问题描述

我在课堂上有这个:public static ArrayList<String> studentInfo = new ArrayList<String>();我创建了一个方法 CreateStudent() 基本上允许用户使用扫描仪创建一个学生,displayStudent() 使用 ObjectInputStream 打开文件并读取数据。现在,当用户创建学生时,它使用 ArrayList 将数据存储到 studentInfo.add() 中,想法是 EditStudent() 访问添加ArrayList 中的数据CreateStudent()添加的信息有没有办法从另一种方法访问该列表?我很感激任何想法,这是我的代码

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.*;
import java.lang.classNotFoundException;
public class MidTermProject  extends ObjectOutputStream {

    private boolean append;
    private boolean initialized;
    private DataOutputStream dout;
    static AtomicInteger idGenerator = new AtomicInteger(0001);
    static int id;
    public static String FullName;
    public static String address;
    public static String city;
    public static String state;
    public static String className;
    public static String instructor;
    public static String department;
    public static String classNumber;
    public static String courseNumber;
    public static String year;
    public static String semester;
    public static String grade;
    public static String studentID;
    public static String courseID;
    public static String enrollmentID;
    
    public static ArrayList<String> studentInfo = new ArrayList<String>();
    
    Scanner keyboard = new Scanner(system.in);
    
    protected MidTermProject(boolean append) throws IOException,SecurityException {
        super();
        this.append = append;
        this.initialized = true;
    }
    
    public MidTermProject(OutputStream out,boolean append) throws IOException {
        super(out);
        this.append = append;
        this.initialized = true;
        this.dout = new DataOutputStream(out);
        this.writeStreamHeader();
    }
    
    @Override
    protected void writeStreamHeader() throws IOException {
        if (!this.initialized || this.append) return;
        if (dout != null) {
            dout.writeShort(STREAM_MAGIC);
            dout.writeShort(STREAM_VERSION);
        }
    }
    
    public static int getId() {
        return id;
    }
    ///Create Student Method
    public static void CreateStudent() throws IOException {

        File file = new File("StudentInfo.dat");
        boolean append = file.exists();
        
        Scanner keyboard = new Scanner(system.in);
        
        try (
                FileOutputStream fout = new FileOutputStream(file,append);
                MidTermProject oout = new MidTermProject(fout,append);
            ) {
            id = idGenerator.getAndIncrement();
            studentID = Integer.toString(getId());
            oout.writeObject("Student ID: " + studentID);
            
            System.out.print("\nPlease enter your @R_640_4045@ion bellow.\n" + "\nFull Name: ");
            FullName = keyboard.nextLine();
            oout.writeObject("Full Name: " + FullName);
            
            System.out.print("Address: ");
            address = keyboard.nextLine();
            oout.writeObject("Address: " + address);
            
            System.out.print("City: ");
            city = keyboard.nextLine();
            oout.writeObject("City: " + city);
            
            System.out.print("State: ");
            state = keyboard.nextLine();
            oout.writeObject("State: " + state + "\n");
            
            studentInfo.add(studentID);
            studentInfo.add(FullName);
            studentInfo.add(address);
            studentInfo.add(city);
            studentInfo.add(state);
        
            oout.close();
            
            System.out.println("Done!\n");
            } catch (FileNotFoundException e) {
                e.printstacktrace();
            } catch (IOException e) {
                e.printstacktrace();
            }
        
    }
    ///Edit Student Method
    public static void EditStudent() throws IOException {
        String editName;
        String editaddress;
        String editCity;
        String editState;
        int editID;
        String editStudent;
        boolean endOfFile = false;
        
        Scanner keyboard = new Scanner(system.in);
        
        System.out.print(studentInfo);
        
        System.out.print("Enter the ID of the student you would like to edit: ");
        editID = keyboard.nextInt();
        String editingID = Integer.toString(editID);
        
        FileInputStream fstream = new FileInputStream("StudentInfo.dat");
        ObjectInputStream inputFile = new ObjectInputStream(fstream);
        
        File file = new File("StudentInfo.dat");
        boolean append = file.exists();
        
        while(!endOfFile)
        {
            try
            {
                FileOutputStream fout = new FileOutputStream(file,append);
                editStudent = (String) inputFile.readobject();
                if(editingID == oout.studentID) {
                    System.out.print("\nPlease enter NEW @R_640_4045@ion bellow.\n" + "\nFull Name: ");
                    editName = keyboard.nextLine();
                    oout.writeObject("Full Name: " + editName);
                        
                    System.out.print("Address: ");
                    editaddress = keyboard.nextLine();
                    oout.writeObject(editaddress);
                        
                    System.out.print("City: ");
                    editCity = keyboard.nextLine();
                    oout.writeObject(editCity);
                        
                    System.out.print("State: ");
                    editState = keyboard.nextLine();
                    oout.writeObject(editState);
                        
                    oout.close();
                        
                    System.out.print("Successfully Edited");
                } else {
                    System.out.print("Error");
                }
            }
            catch (EOFException | ClassNotFoundException e)
            {
                endOfFile = true;
            }
        }
    }
    display Student Method
    public static void displayStudent() throws IOException {
        FileInputStream fstream = new FileInputStream("StudentInfo.dat");
        ObjectInputStream inputFile = new ObjectInputStream(fstream);
        
        String student;
        boolean endOfFile = false;
        
        while(!endOfFile)
        {
            try
            {
                student = (String) inputFile.readobject();
                System.out.print(student + "\n");
            }
            catch (EOFException | ClassNotFoundException e)
            {
                endOfFile = true;
            }
        }
        System.out.println("\nDone");
        
        inputFile.close();
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)