tags:

views:

194

answers:

3
        while( inStream.hasNextLine() )
        {
                ...

                lineList.add( inStream.nextLine() );
        }
        ...

lineList is an ArrayList. The code is reading everything nicely except it won't grab the last line. The last two lines in the text file end like this:

"a sentence here..."
<a blank line here. the blank line is the last line>

I'm assuming it won't grab it since hasNextLine() is not detecting another line after this one?

What's a way to grab the last line? I thought reading until it was EOF and then catching the exception might work but there doesn't seem to be a way to do that.

EDIT: MORE INFO

public void readLines()
{
    lineList = new ArrayList();

    try
    {
        inStream = new Scanner( new File( fileName ) );
    }
    catch( FileNotFoundException e )
    {
        System.out.println( "Error opening the file. Try again." );
    }


    if ( inStream != null )
    {
        while( inStream.hasNextLine() )
        {
            ++originalLines;

            lineList.add( inStream.nextLine() );
        }
        inStream.close();
    }
}

There's the whole method. Anything wrong?

EDIT: EVEN MORE INFO

public static void main(String[] args)
{
    Scanner inStream = null;
    String test = "";

    inStream = new Scanner( test );

    while( inStream.hasNextLine() )
    {
        System.out.println( inStream.nextLine() );
    }
}

It will not pick up an empty string but it will pick up a whitespace " "

+1  A: 

That code works fine for me -- are you sure there's actually a blank line at the end (as in, the file ends in two newlines, not just one)?

Michael Mrozek
Yeah. Well, I don't if you can call it a blank line. It's more like an empty string? There's nothing in it. For example if on the last line you set the cursor to it and pressed the right arrow key, it wouldn't move.
ShrimpCrackers
+3  A: 

nextLine() works just fine to return the last line, even if it's blank. I suspect your problem lies elsewhere.

String multilinetext =
    "Line1\n" +
    "Line2\n" +
    "\n" +
    "Line4\n" +
    "\n";
Scanner sc = new Scanner(multilinetext);
while (sc.hasNextLine()) {
    System.out.println("[" + sc.nextLine() + "]");
}
/* prints 
[Line1]
[Line2]
[]
[Line4]
[]
*/

It will not pick up an empty string

This is the correct behavior by design. If the input is an empty string, you can't expect a Scanner to say hasNextLine() and return an empty string on nextLine(), especially since this doesn't advance the Scanner past any character.

If you think about it, if you expect a Scanner to say hasNextLine() on an empty string, and return an empty string on nextLine(), then this would result in an infinite loop: it will always hasNextLine() and it will always return an empty string on nextLine() forever. Such behavior is clearly impractical.

polygenelubricants
I've added the whole method to my OP. Can you check it?
ShrimpCrackers
Method looks fine to me. Use `wc -l` or a hex editor etc to see what the file really contains. Doing the "if I press the right arrow key it wouldn't move" test is useless because that relies on the text editor's behavior and not what the actual file contains.
polygenelubricants
Thanks. My guess is that it is not a delimiter but an empty string. If my guess is correct, how would I pick up an empty string? See added code above.
ShrimpCrackers
Thanks. That makes sense. Is there a way to retrieve that empty space though?
ShrimpCrackers
You can just grab it yourself. `String emptyString = "";` Then do whatever you want with `emptyString`. I don't think you can do a lot with it, though; an empty string is really not that interesting.
polygenelubricants
aloh, as mentioned in my late answer I think if you see an extra line in your editor is merely the cursor pointing to where new text would appear. If you really want it take polygenelubricants advice (taken a bit further) and add lineList.add( "" ); outside the while.
Philip T.
+1  A: 

Scanner won't pick up an empty String. There isn't anything to tokenize. Scanner is a tokenizer, defaulted to use Character.isWhitespace according to the Java 6 javadoc. Every time the token is encountered everything before it is returned as a string, but not including the token. If the last item is the token, it is removed and no new line is added. You might end up with empty strings in your ArrayList, but it is only an empty string, because the token has been removed. No data and no token and scanner doesn't see a line. What you are seeing in your editor isn't a blank line. Your cursor is at the point a character will be added if you type. If you don't type anything there isn't an actual line there.

Philip T.
Thank you. That makes sense. I suppose I could just leave it out.
ShrimpCrackers