• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

skip blank lines in reading from text file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 24861
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a blank line:
" "
(Excluding quotes)?
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a stupid question at all, see very few of those things around
Glad we can help!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic