• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to Disregard Lines That Are Just Whitespace When Using A Reader

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am trying to read in the contents of a textfile line by line.

I need to process only lines that contain characters when I read it in as so:



This seems like a simple problem, but I cannot find the conditional that would determine a blank line. I am a beginner with regex too.

Please help.

Sincerely,
J
 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol found it! Tested and worked:




 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, that's much better than a regex, isn't it?

You could also do it like this (pretty much the same):



 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Yeah, that's much better than a regex, isn't it?

You could also do it like this (pretty much the same):





The thread title specifies "Whitespace" which according to the Pattern class (I know not really a definitive source but the best I can find without too much research) is the character set [ \t\n\x0B\f\r] but the OP body implies just a simple space. If indeed the OP just wants to handle spaces and not white spaces then the responses so far are probably the best approach but if white spaces are to be white spaces in the regex sense then



could be used. Since the readLine() method does not return either '\r' or '\n' (they are treated as line terminators) then this actually more than is needed but it will match any empty string.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:If indeed the OP just wants to handle spaces and not white spaces then the responses so far are probably the best approach but if white spaces are to be white spaces in the regex sense then...


Actually, if you look at the docs for String.trim(), you'll see that it removes all characters at or below '\u0020' (the space character) from the start and end of a String, which is pretty darn close to regex's interpretation of whitespace (although, as you say, not exactly the same).

BTW, Thanks Paul. I hadn't even noticed that 1.6 added the isEmpty() method. Amazing what you miss sometimes.

Winston
 
Marshal
Posts: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But isEmpty() is (I think) something simple like this:-Yes, the Pattern class is the definitive authority about what counts as whitespace … at least in Java regexes.

And while I was looking for the Pattern link, I found there is a Phaser class. I didn’t check whether there is a Klingon class.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic