1

I am buffering a text file of into 'arraylist lines' i then need to split each line into a new arrayList parts, so that i can find information from each line and add the data to a model i have built, the reason i am using arrayLists is because of there expandable properties, meaning i wont need to worry about the size of either the line or the text file.

the code is below:

    try(BufferedReader buffer = 
            new BufferedReader(new FileReader("src/Sample.txt")))
    {
        String currentLine;
        ArrayList<String> lines = new ArrayList<String>();
        ArrayList<String> parts = new ArrayList<String>();
        //ListIterator<String> lineItr = lines.listIterator();

        while((currentLine = buffer.readLine()) != null)
        {
            lines.add(currentLine);

            for(String line : lines)
            {
                parts.addAll(line.split("\\s+"));

            }


            //lineItr.next();
            //lineItr.set(currentLine);

            //System.out.println(lineItr.next());
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

}

i am having my troubles with parts.addAll(line.split("\s+"); i do not understand why the statement does not iterate through lines, splitting and adding each part of the string to the parts array list, am i misunderstanding something here?

thanks Babble

5 Answers 5

3

list.addAll() accepts a java.util.Collection where as str.split returns you an array is not a collection. Hence you can not add it directly to a list. You need to convert into a list first.

for(String line : lines)
{
   parts.addAll(Arrays.asList(line.split("\\s+"));
}
Sign up to request clarification or add additional context in comments.

Comments

2

String.split() returns Array of String . So you have to use Arrays.asList() to convert it into list .

parts.addAll(line.split("\\s+"));

Above line should be:

parts.addAll(Arrays.asList(line.split("\\s+")));

Or :

Collections.addAll(parts, line.split("\\s+"));

3 Comments

Thanks for answer an explanation :), what is collections?
@Babbleshack See this link to understand better.
@Babbleshack No. Click on "this" on my previous comment.
2

try this

parts.addAll(Arrays.asList(line.split("\\s+")));

Comments

1

List.addAll accepts a Collection but line.split("\\s+") returns String[]. You can do it this way

parts.addAll(Arrays.asList(line.split("\\s+")));

Comments

0

Every time a new line arrives, you append the whole thing all over again. Drop the inner "for" loop, and just split currentLine instead.

EDIT based on comment:

The Java Way would be implementing the year and month objects as containers. The simpler alternative is using string-keyed maps.

5 Comments

That's a good point, problem is i want to incorporate a second data set, and if that data set contains a different layout, then i wont be able to handle it.
I don't think I understand :-)
haha, okay well, i have a data set. which is a text file. each line has a year, then a month, then a collections of days, i want to load that data into an arraylist of year obj, which contains an array list of month obj, which in turn contains a list of days. If the format of the data file change i would have a problem loading the data in if i didn't use the lines list.
Oh, that's a completely different question then :-) I'll edit my answer.
ill look into string-keyed maps when i get back from my lecture

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.