2

I have a text file that looks like this.

BEGINNING OF LIST

Name: Janet
Age: 21
Birthday month: April

END OF LIST

BEGINNING OF LIST

Name: Peter
Age: 34
Birthday month: January

END OF LIST

So I want to grab info and put it into an object array. it is an extensive list and I am using the delimiters beginning of list and end of list to delimit the content.

How can I store these items in an object array?

2
  • What exactly is the problem with this task? Don't yiu know how to read files? Or how to parse a string? Or how to create lists? Or how to create a model class? Commented Jul 13, 2015 at 0:19
  • I can read the files and create a model class. I just need to know how to separate it based on "THE BEGINNING OF LIST" and "END OF THE LIST" there is going to be a lot of useless info being read Commented Jul 13, 2015 at 0:58

1 Answer 1

2

I would suggest you create a class first for storing the information, with name, age, and birthday month attributes. It's a very good practice to override the toString() method so you can print out the class neatly.

Then you can check for each line whether it contains information about the name, age, or birthday month through splitting each line into an array of words, and checking for the information.

Once the line reads "END OF LIST", you can add a class Person with the parameters to the ArrayList.

For the example I used "people.txt" as the file (make sure you place the text document outside of the src folder which contains your .java files).

Main.java

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args)
    {
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        String name = null;
        String age = null;
        String month = null;
        List<Person> people = new ArrayList<Person>();

        try
        {
            String fileName = "people.txt";
            fileReader = new FileReader(fileName);
            bufferedReader = new BufferedReader(fileReader);
            String line = null;

            while ((line = bufferedReader.readLine()) != null)
            {
                String[] information = line.split(" ");

                if (Arrays.asList(information).contains("Name:"))
                {
                    name = information[1];
                }
                if (Arrays.asList(information).contains("Age:"))
                {
                    age = information[1];
                }
                if (Arrays.asList(information).contains("month:"))
                {
                    month = information[2];
                }
                if (line.equals("END OF LIST"))
                {
                    people.add(new Person(name, age, month));

                    name = "";
                    age = "";
                    month = "";
                }
            }

            for (Person person : people)
            {
                System.out.println(person);
                System.out.print("\n");
            }
        }
        catch (FileNotFoundException e) 
        {
            System.out.println(e.getMessage());
        } 
        catch (IOException ex) 
        {
            System.out.println("Error reading people.txt");
        }
        finally
        {
            if (bufferedReader != null)
            {
                try
                {
                    bufferedReader.close();
                }
                catch (IOException ex)
                {
                    System.out.println(ex.getMessage());
                }
            }
            if (fileReader != null)
            {
                try
                {
                    fileReader.close();
                }
                catch (IOException ex)
                {
                    System.out.println(ex.getMessage());
                }
            }
        }       
    }
}

Person.java

public class Person {
    private String name;
    private String age;
    private String birthday;

    public Person(String name, String age, String birthday)
    {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString()
    {
        String information = "Name: " + name + "\nAge: " + age + "\nBirthday: " + birthday;
        return information;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@SantanaMax Your welcome! If you think my answer is good, please accept it by clicking the green checkmark.
thanks. Just a quick question. If the "line" is null, then why do we split it up using spaces and how can I also delimit using "BEGINNING OF LIST" There is going to be a lot of useless info in between the lists.
@SantanaMax line is set to null at the beginning because we haven't read anything yet. We read the lines in the while loop so it isn't null when we start the file operation. The program I gave you actually ignores all the useless information between the list because it checks if the line actually contains information or not.
Ok thanks! Sorry I am a beginner so I am trying hard to understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.