Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Skipping a blank line

 
Darrell Morello
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I make sure that empty lines are skipped when reading a file. I mean if the user has hit the return key twice after a line, and then started the next line, how do I make sure that the empty line is not read? I also want to ignore any line that has anything other than a letter. So numbers also off.

For example, the file can be


Heres my code so far


Thanks

[ May 25, 2005: Message edited by: Darrell Morello
[ May 26, 2005: Message edited by: Darrell Morello ]
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you process the inputStream, between your while(line != null){ and your line = inputStream.readLine(), you should just jump over any lines that you don't need, according to thejavadocs, it says:

readLine
public String readLine()
throws IOExceptionRead a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs


So what happens when you read a blank line? You would get a String with length 0. So, just don't process lines of length 0, or if they start with anything other than a letter.
 
Fahd Shariff
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To check for digits:
 
Darrell Morello
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic