File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Reading a text file and parsing into arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a text file and parsing into arraylist" Watch "Reading a text file and parsing into arraylist" New topic
Author

Reading a text file and parsing into arraylist

Shaun Hanson
Greenhorn

Joined: Nov 11, 2012
Posts: 4
Hi -

I am reading a text file of student names with test scores. Each line of data is separated by a blank line. I am adding the students into an array list and parsing the fields into columns. My code for this is below and it works perfectly if I remove the blank lines in the text file, but when I keep the blank lines I am getting an ArrayIndexOutOfBoundsException. FIELD_SEP is defined as " " since the records are delimited by a space (e.g. lastname firstname score)

How do I skip these blank lines, given my code below? What am I doing wrong? THANKS!





Note that I have tried adding and if statement to check for length == 0 but I just get into an endless if/while loop.

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

If a blank line contains spaces or tabs, that condition won't do the job. Try to trim the line first to remove these:


The quieter you are, the more you are able to hear.
Shaun Hanson
Greenhorn

Joined: Nov 11, 2012
Posts: 4
Thanks but that didn't work - I'm still getting into an endless loop. Moving from While to If to Continue to While etc. Is there some type of next line code I need to add to break out of this loop?

When I debug and I am in this endless loop, the variable value of the line (the blank I assume) is ""



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Can you show your current code please?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Of course it is, didn't notice it before. You are reading line before you enter while loop, so when you do continue; when line is empty it enters while loop with the same empty line again (because you read next at line 19, and that statement is not reached because of continue;).

What should be done is to remove that statement at line 19 and read file line by line at the beginning of the while:
Shaun Hanson
Greenhorn

Joined: Nov 11, 2012
Posts: 4
This is my class

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Looks like Kemal has nailed it.
Shaun Hanson
Greenhorn

Joined: Nov 11, 2012
Posts: 4
Yes! Thank you!!!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Jeff Verdegan wrote:Looks like Kemal has nailed it.

I hate when I miss something just because I assume it is done the way I would do that. Didn't notice the problem the first time! Shame on me!

Yes! Thank you!!!

You're welcome, and welcome to the Ranch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a text file and parsing into arraylist