0

I tried to figure out the following problem for the last 20 hours, so I thought before I start thinking about jumping out of the window ;-), I better ask here for help:

I have a text file with following content:
ID
1
Title
Men and mice
Content
Lenny loves kittens
ID
2
Title
Here is now only the Title of a Book
ID
3
Content
Here is now only the Content of a Book

The problem as you can see is that there is either both title and content after id or only title after id. I want to create text files which contain an ID value (for example 1) and the corresponding title value and/or content value.

The best I achieved was three lists. One with id values, one with title values and one with content values. But it is actually useless, because the information between id, content and title is lost.

I would really appreciate your held.

3
  • 2
    Please show the code of what you tried already, and samples of what the output files should look like given the example input file you have included. Commented Dec 31, 2015 at 0:25
  • Do not lose "the information between id, content and title". Read the id, title, content, write them to the output, then move on to the next entry. You don't need any lists. Commented Dec 31, 2015 at 0:28
  • just keep the string and the id, and split and read the contents of the whole string when you need it Commented Dec 31, 2015 at 0:30

2 Answers 2

2

So you want to populate a collection of a class with three fields.

class Data {
    int id;
    String title;
    String content;

    // helper method to read a file and return a list.
    public static List<Data> readAll(String filename) throws IOException {
        // List we will return.
        List<Data> ret = new ArrayList<Data>();
        // last value we added.
        Data last = null;
        // Open a file as text so we can read the lines.
        // us try-with-resource so the file is closed when we are done.
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            // declare a String and use it in a loop.
            // read line and stop when we get a null
            for (String line; (line = br.readLine()) != null; ) {
                // look the heading.
                switch (line) {
                    case "ID":
                        // assume ID is always first
                        ret.add(last = new Data());
                        // read the next line and parse it as an integer
                        last.id = Integer.parseInt(br.readLine());
                        break;

                    case "Title":
                        // read the next line and save it as a title
                        last.title = br.readLine();
                        break;
                    case "Content":
                        // read the next line and save it as a content
                        last.content = br.readLine();
                        break;
                }
            }
        }
        return ret;
    }
}

Note: the only field which matters is ID. Content and Title are optional.

To get from 20 hours down to 5 minutes, you need to practice, a lot.

Sign up to request clarification or add additional context in comments.

2 Comments

This helps a lot. Thank you. I forgot to mention, that that the titel and content can have multiple lines. Do you have an idea, how to change the code in regards to that? I saw that someone can use a pattern to extract the lines between two defined Strings, but I don't know how to implement that. Could you please help?
@javanoob You could either create a List or values or simply combined them with a new line in between. The latter is likely to be simpler. I suggest you try it.
1

You can keep "the information between id, content and title" in your program if you create a Book class and then have a list of Book instances.

Book class:

public class Book {

    private int id;
    private String title;
    private String content;

    //...
    //getters and setters

}

List of books:

private List<Book> books = new ArrayList<Book>();

Comments

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.