简体   繁体   中英

How to read values from a text file into hashmap in Java?

I have created class Patient (pojo), where I have declared variables.

I have added getter and setter methods, as well as a constructor:

 public class Patient {
    private String patientName;
    private String phoneNumber;
    private  int age;

    //generate getter and setter method
    public String getPatientName() {
        return patientName;
    }
    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    //generate constructor
    public Patient(String patientName, String phoneNumber, int age) {
        this.patientName = patientName;
        this.phoneNumber = phoneNumber;
        this.age = age;
    }
}

I have created an interface PatientDetails and implemented the methods in the Hospital class.

public interface PatientDetails {
    public void addpatient();
    public void refreshPatient()throws IOException; 
}

Here is how the methods are implemented:

    public class Hospital implements PatientDetails {`
Scanner scan = new Scanner(System.in);
    int token = 0;
    String name, mobileNumber;
    static HashMap<Integer, Patient> map = new HashMap<Integer, Patient>();
    File file = new File("E:\\Patient\\pt.txt");
    int age;

    public void addpatient() {

        BufferedWriter bufferedWriter = null;
        FileWriter fileWriter = null;
        try {
            // true = append file
            // write a data in a file
            fileWriter = new FileWriter(file, true);
            bufferedWriter = new BufferedWriter(fileWriter);

            System.out.println("Enter the name");
            scan.nextLine();
            name = scan.nextLine();

            System.out.println("Enter Mobile number must be 10 digit");
            mobileNumber = scan.nextLine();

            System.out.println("Enter the age");
            age = scan.nextInt();


            bufferedWriter.write("TokenNumber:" + token + "," + "PatientName:" + name + ",PhoneNumber:" + mobileNumber
                    + ",Age :" + age + ";");
            // for nextline
            bufferedWriter.newLine();
            // close file
            bufferedWriter.close();
            fileWriter.close();
            System.out.println("yours Appoint cofirmed....\nPatient Name: " + name + "\nMobile number: " + mobileNumber
                    + "\nToken number is: " + token + "\nAge is:" + age);
            token++;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Something went wrong");
            e.printStackTrace();
        }
    }

    @Override
    public void refreshPatient() throws IOException {
        Patient patient=new Patient(mobileNumber, mobileNumber, age);
        String filePath = file.getPath();
        System.out.println("refreshed successfully");

        String line;
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        map=new HashMap<>();
        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(":", 2);
            if (parts.length >= 2) {
                String key = parts[0];
                String value = parts[1];
                //map.put(Integer.parseInt(key), value);

            } else {
                System.out.println("ignoring line: " + line);
            }
        }

            System.out.println(map);
        reader.close();
    }`)

I have added the patient name, age, and mobile number into the patient.txt file.

When I call the refresh method all the values should come to the map, but I am not getting the Patient class values into the map.

How to fix that?

你应该分裂,:

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