Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将患者信息存储到文本文件中,然后加载_Java_Arrays_Arraylist_Text Files - Fatal编程技术网

Java 将患者信息存储到文本文件中,然后加载

Java 将患者信息存储到文本文件中,然后加载,java,arrays,arraylist,text-files,Java,Arrays,Arraylist,Text Files,我们的任务是编写一个简单的医院管理系统。用户可以输入患者信息,如姓名、性别、出生日期和疾病 数据存储在ArrayList中,并保存到文本文件中 现在我的问题是:我需要显示输入系统的所有患者但我无法以正确的方式获取数据并将其显示在控制台窗口中输出内容应为:患者姓名、性别、疾病、出生日期 这是我的。。它不是全部代码,只是部分代码。 这是主课 import java.io.*; import java.util.*; public class Main { public static void m

我们的任务是编写一个简单的医院管理系统。用户可以输入患者信息,如姓名、性别、出生日期和疾病
数据存储在ArrayList中,并保存到文本文件中

现在我的问题是:我需要显示输入系统的所有患者
但我无法以正确的方式获取数据并将其显示在控制台窗口中
输出内容应为:患者姓名、性别、疾病、出生日期

这是我的。。它不是全部代码,只是部分代码。
这是主课

import java.io.*;
import java.util.*;

public class Main {

public static void main (String args []) {

    int choice = 0;

    List<Patient> PatientList = new ArrayList<Patient>();       
    Patient patient = new Patient();        
    List<Doctor> DoctorList = new ArrayList<Doctor>();
    Doctor doctor = new Doctor();

    Scanner readInput = new Scanner(System.in);
    Scanner readChoice = new Scanner(System.in);        


    do {
            System.out.println("Press 1 to enter a new patient \nPress 2 to enter a new doctor \nPress 3 to show all patients \nPress 4 to show all doctors \nPress 0 to quit.");   
            choice = readChoice.nextInt();

        switch (choice) {

            //case 0: 

            case 1: System.out.println("Please enter the name of the new patient: ");
                    patient.setPatientName(readInput.nextLine());


                    System.out.println("Please enter the gender of the new patient: ");
                    patient.setPatientGender(readInput.nextLine());

                    System.out.println("Please enter the disease of the new patient: ");
                    patient.setDisease(readInput.nextLine());       

                    System.out.println("Please enter the age of the new patient: ");
                    patient.setPatientDateOfBirth(readInput.nextLine());

                    PatientList.add(patient);

            try {
                FileOutputStream fos = new FileOutputStream("patients.tmp");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(PatientList);
                oos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                    break;      

/////////////////////////////////////////////////////////////////   
//case 2 is not relevant to my question therefore I did not put it in here

            case 3: if(PatientList.size() == 0) {

                        System.out.println("\nNo patients were found...\nReturning to main menu...\n");     

                    } 

                    else {  


                            try {
                                FileInputStream fis = new FileInputStream("patients.tmp");
                                ObjectInputStream ois = new ObjectInputStream(fis);
                                PatientList =(ArrayList<Patient>) ois.readObject();
                                ois.close();
                                fis.close();
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (ClassNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                        for (int i = 0; i < PatientList.size(); i++) {                              

                            System.out.println(PatientList.get(i).getPatientName()+", "+PatientList.get(i).getPatientDateOfBirth()+", "
                            +PatientList.get(i).getPatientGender()+", "+PatientList.get(i).getDisease());                       

                        }

                    }                       
                    break;
我猜在主课(案例3)中有一些错误,但我自己解决不了


我非常感谢你的帮助

它在第一次启动时从不从磁盘加载数据的原因是,案例3中的第一个if语句检查PatientList.size是否为空,如果为空,则不从磁盘加载数据(else语句仅在列表中有问题时运行)

要解决这个问题,您可能需要检查PatientList.isEmpty()是否正确,如果正确,则尝试从磁盘加载数据,如果没有数据,则打印出未找到的记录

case 3: 
    if(PatientList.isEmpty()) 
    {
        try 
        {
            //load data from the hard drive
        } catch (FileNotFoundException e) {
        } catch (ClassNotFoundException e) {
        } catch (IOException e) {
        }

        //populate PatientList if data was loaded from disk here

        //if PatientList is still empty then print out no patients found
        if(PatientList.isEmpty())
        {
            System.out.println("\nNo patients were found...\nReturning to main menu...\n");
        }           
    }

break;

您得到的输出是什么(与预期输出进行比较)?在案例3中,您应该检查`if(PatientList.isEmpty()){}`这可能无法解决您的问题,但这只是使用collection时应该编写的内容之一。另外,最好使用try with resources或使用finally块来关闭流。如果PatientList.size(),则只能从磁盘加载数据0是有意的吗?我想这是检查列表中是否有可以加载的内容的方法。我删除了if/else语句,现在可以加载数据了。有人能给我解释一下吗?我应该使用if(PatientList.isEmpty())吗?好吧,我想我知道问题出在哪里了。。当程序启动时,数组将始终为空,因为到目前为止没有加载任何数据。是吗?@r4ZR2810我发布的修复代码中有一个小错误。现在它已经被修复了。我几乎尝试了所有的方法将文件读入ArrayList,但是我没有注意到我在if语句中犯的错误。谢谢!
case 3: 
    //this if statment is checking if PatientList.size() is 0 or in other words check if it is empty
    if(PatientList.size() == 0) 
    {
        System.out.println("\nNo patients were found...\nReturning to main menu...\n");     
    }
    //this else code block will only run if the above if statment is false
    else 
    {  
        try 
        {
            //load data from the hard drive
        } catch (FileNotFoundException e) {
        } catch (ClassNotFoundException e) {
        } catch (IOException e) {
        }
    }                       
break;
case 3: 
    if(PatientList.isEmpty()) 
    {
        try 
        {
            //load data from the hard drive
        } catch (FileNotFoundException e) {
        } catch (ClassNotFoundException e) {
        } catch (IOException e) {
        }

        //populate PatientList if data was loaded from disk here

        //if PatientList is still empty then print out no patients found
        if(PatientList.isEmpty())
        {
            System.out.println("\nNo patients were found...\nReturning to main menu...\n");
        }           
    }

break;