[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: How to detect a "blank" line.



>Hi,
>
>In your java program, exchange the line
> if (s1.charAt(0)!='#') { // not a comment or NULL line
>with
> if (s1.equals("") || s1.charAt(0)!='#') { // not a comment or NULL line
>
>Explanation:
>You get empty lines as empty strings from readLine(). So you first have
>to check whether the string s1 is empty. This is done with
>s1.equals(""). If this yields true, the evaluation of the conditionalin
>the if-statement is terminated, you get true. I the string is not empty,
>the evaluation is continued. Now, that you know that the string is NOT
>empty, you can check for the first character. If you try to check the
>first character without knowing for shure that the string is not empty,
>this will throw errors, since empty strings have no characters.
>
There's a slight problem with this too. What if the so-called empty line is
not just a '\n' but  a few whitespaces followed by a '\n'? I think it's a
better option to use a StreamTokenizer or a StringTokenizer. That way, you
eliminate the whitespaces.

Akshat


- --------------------------------------------------------------------
For more information on Linux in India visit http://www.linux-india.org/
Please do not post HTML email to this mailing list.  HTML mails will be
thoroughly ignored and derisively sniggered at in private.

------------------------------