Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:51:10+00:00 2026-06-12T15:51:10+00:00

I am stuck with this problem, please help: I want to read a txt

  • 0

I am stuck with this problem, please help:
I want to read a txt file and put the content into an ArrayList, and the format is like this:

name                Yoshida Shuhei
birthday            8-04-1961
phone               0123456789
email               abc@123.com
medicalHistory      None
address             12 X Street, Suburb, 
                    NSW, Australia


address             13 Y Street, Suburb, VIC, Australia
name                Kazuo Hirai
medicalHistory      None
email               xyz@123.com
phone               0987654321
birthday            26-11-1972

the file contains several patient records, and the record within each patient’s records block may occur in any order(e.g. the first patient’s name comes first, but the second patient’s address comes first), and all records are separated by blank lines.

My idea is, if the current line is not a blank line, begin to read the patient records and add them into a patient object, here’s my code:

public static ArrayList<Patient> getData(String fileName) {
        try {
                File file = new File(fileName);
                Scanner reader = new Scanner(file);
                ArrayList<Patient> recordList = new ArrayList<Patient>();
                ArrayList<MedicalHistory> mhList = new ArrayList<MedicalHistory>();
                int index = -1;
                int mh_index = -1;
                String s;
                Patient p = null;
                MedicalHistory mh = null;
                boolean addressActive = false;
                boolean mhActive = false;

                while (reader.hasNext()) {
                    s = reader.nextLine();
                    Scanner line = new Scanner(s);
                    String cmd;

                    if (!s.trim().isEmpty()) {
                        cmd = line.next();

                        if (cmd.equalsIgnoreCase("name")) {
                            index++;
                            p = new Patient();
                            p.setName(line.nextLine());
                            recordList.add(index, p);
                            addressActive = false;
                            mhActive = false;

                        } else if (cmd.equalsIgnoreCase("address")) {
                            if (line.hasNext()) {
                                p.setAddress(line.nextLine().trim());
                                recordList.set(index, p);
                            }
                            addressActive = true;
                            mhActive = false;

                        } else if (cmd.equalsIgnoreCase("birthday")) {
                            p.setBirthday(line.nextLine());
                            recordList.set(index, p);
                            addressActive = false;
                            mhActive = false;

                        } else if (cmd.equalsIgnoreCase("email")) {
                            if (line.hasNext()) {
                                p.setEmail(line.nextLine());
                                recordList.set(index, p);
                            }
                            addressActive = false;
                            mhActive = false;

                        } else if (cmd.equalsIgnoreCase("phone")) {
                            if (line.hasNextInt()) {
                                p.setPhone(line.nextInt());
                                recordList.set(index, p);
                            }
                            addressActive = false;
                            mhActive = false;

                        } else if (cmd.equalsIgnoreCase("medicalHistory")) {
                            mh = new MedicalHistory();
                            //...parse the medicalHistory
                            addressActive = false;
                            mhActive = true;

                        } else if (addressActive) {
                            String address = p.getAddress() + " " + s.trim();
                            p.setAddress(address);
                            recordList.set(index, p);

                        } else if (mhActive) {
                            //to deal with multiple medical histories
                        } else
                            System.out.println("Error: no command:" + s);
                    }
                }
                reader.close();
                return recordList;
            } catch (Exception e) {
                System.out.println("Error:- " + e.getMessage());
                return null;
            }
    }

The problem is, my code can only handle when the name comes first; if the first non-empty line begins with other command(e.g. begins with address), there will be no new Patient() initialized for it, and the program will get an error…

So where exactly should I put the p = new Patient() in, the program to read the patient records no matter what the order of commands is, and then store the data in a Patient object?

Can anybody be able to improve my code and satisfy this condition? Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:51:12+00:00Added an answer on June 12, 2026 at 3:51 pm

    I’d suggest that you read each block into a HashMap<String,String> that maps each attribute to its value from the file. When the block is finished (that is, when you see a blank line or the end of the file) you can process the block in whatever particular order of attributes you need so as to properly create a Patient object.

    Alternatively, with your current logic, you only need to change it a bit to do what you want:

    . . .
    while (reader.hasNext()) {
        s = reader.nextLine();
        Scanner line = new Scanner(s);
        String cmd;
    
        if (!s.trim().isEmpty()) {
            if (p == null) {
                // starting a new block -- create a new patient record
                p = new Patient();
                recordList.add(p);
            }
    
            if (cmd.equalsIgnoreCase("name")) {
                index++;
                p.setName(line.nextLine());
                addressActive = false;
                mhActive = false;
            } else if (cmd.equalsIgnoreCase("address")) {
                if (line.hasNext()) {
                    p.setAddress(line.nextLine().trim());
                }
                addressActive = true;
                mhActive = false;
    
            } else if (cmd.equalsIgnoreCase("birthday")) {
                p.setBirthday(line.nextLine());
                addressActive = mhActive = false;
            } else if (cmd.equalsIgnoreCase("email")) {
                if (line.hasNext()) {
                    p.setEmail(line.nextLine());
                }
                addressActive = mhActive = false;
            } else if (cmd.equalsIgnoreCase("phone")) {
                if (line.hasNextInt()) {
                    p.setPhone(line.nextInt());
                }
                addressActive = mhActive = false;
            } else if (cmd.equalsIgnoreCase("medicalHistory")) {
                mh = new MedicalHistory();
                //...parse the medicalHistory
                addressActive = false;
                mhActive = true;
            } else if (addressActive) {
                String address = p.getAddress() + " " + s.trim();
                p.setAddress(address);
            } else if (mhActive) {
                //to deal with multiple medical histories
            } else
                System.out.println("Error: no command:" + s);
            }
        } else {
            // blank line indicates end of block
            p = null;
        }
    }
    . . .
    

    Note that when you modify the current patient record (referenced by p), you don’t need to set the element of recordList again; it will be automatically updated, since it refers to the object already in the array list. With this, you don’t need the index at all; you just add the new patient record to the end of recordList and keep modifying it as long as the input is still in the same block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently stuck on this problem. I've hooked into the method_missing function in a
I'm hoping someone can help me as I've been stuck on this problem for
I have been frustrated by this for like a day...please help. I have an
can someone please help me out with this problem i'm having while building my
Hi i got stuck with this problem, i can't find out how to get
So I'm stuck on this problem where I've been asked to write an function
I am currently stuck at this problem right now where I don't seem to
I got stuck in this problem for an hour. I am thinking this is
I got stuck with this problem. I wrap two tables inside of a h:form.
I am stuck with this problem for 3 days with no avail to solve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.