• Post Reply Bookmark Topic Watch Topic
  • New Topic

skip blank lines in reading from text file  RSS feed

 
Sebastiano Barbieri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I did some research on the topic not beleaving I couldn't find anything, but maybe it's just an unlucky day. What I'm trying to do is simply to get my program to skip blank lines when reading from a text file. For now I'm using something like:
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
while ((line = in.readLine()) != null) { //do something}
Any suggestions?
Thanx in advance.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could put an if block at the beginning of the processing that tests if the Strings length is not equal to zero. Since a blank line returns an empty String, processing would be skipped for those lines.
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a blank line:
" "
(Excluding quotes)?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ok, I was going under the assumption that a blank line was being inserted by some method such as writer.println(); and not some empty String of variable length was being used for some bizarre reason.
But to be absolutely safe, Tim's method is better (dagnabit!) Though I'd rather stick the processing in an if block and avoid having a continue, but that's probably from my Cattle Drive training. I only use continues if absolutely necessary, and here I don't see it.
Who let you in here anyways?
[ July 21, 2003: Message edited by: jason adam ]
 
Sebastiano Barbieri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh man! all right, I'm quite new to java and when I had a blank line (carriage return) in the file I was reading I got a null pointer exception. My first thought was that the while loop breaked when it found an empty line. Now I see I was wrong and that the error was probably that I tokenize the lines coming in and the program is trying to tokenize an empty string, which is probably why I get the exception? Anyway with an if statement at the beginning the problem is solved. Thanx a lot to both of you.
Ps hehe guess I couldn't find a lot cause my question was stupid
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a stupid question at all, see very few of those things around
Glad we can help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!