• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a text file and parsing into arraylist  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a blank line contains spaces or tabs, that condition won't do the job. Try to trim the line first to remove these:
 
Shaun Hanson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ""



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show your current code please?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my class

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like Kemal has nailed it.
 
Shaun Hanson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! Thank you!!!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!