0

I have a text file which is read in. It has delimeters which are <.> . There is a main subject then there are three paragraphs. Lets say title, section1, section2, section3, and then the next article.

How can I store the data so that ArrayList one will have all the titles, ArrayList 2 will have all the section1 information, etc.? I want be able to output these arrays.

E.g.:
Large storm on its way.

about the large storm

statics on storms

conclusions about storms

The example above shows what one record would look like.

public void read()
{
    try
    {
        FileReader fr = new FileReader(file_path);
        BufferedReader br = new BufferedReader(fr);
        String s = "";
        // keep going untill there is no input left and then exit         
        while((s = br.readLine()) != null)
        { }
        fr.close();
    }
    catch (Exception e)
    {
        System.err.println("Error: read() " + e.getMessage());
    }
}

public static void main(String [] args)
{
    Reader reader = new ResultsReader("C:/data.txt");
    reader.read();
    String output = ((ResultsReader)reader).getInput();
    String str = "title<.>section1<.>section2<.>";
    String data[] = str.split("<.>");   
}

I am not sure how to store the data in separate ArrayLists so that they can be traversed.

0

1 Answer 1

1

You cannot create arrays and put the data into them, because you don't know how large to create the arrays. So, use a list instead and then turn them into arrays after you have finished reading the file:

List tilesList = new ArrayList<String>();
// etc.

FileReader fr = new FileReader(file_path);
BufferedReader br = new BufferedReader(fr);
String s = null // I think this should be null, so that if there are no lines, 
                // you don't have problems with str.split();
while((s = br.readLine()) != null) {
  String[] line = str.split("<.>");
  tilesList.add(line[1]);
  // etc.
}
fr.close();

String[] tiles = tilesList.toArray(new String[tilesList.size()]);
// etc.
Sign up to request clarification or add additional context in comments.

1 Comment

i should have added arraylist my mistake

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.