Re: Scanner class and last line in a file



On Mon, 25 Sep 2006 10:03:55 -0700, dagon@xxxxxxxxx (Mark Rafn) wrote:

DemonWasp <jda980@xxxxxxx> wrote:
What I *was* asking was "how do I get the last line from a file, even
when there is no trailing whitespace?"

I can't repro the problem - help me understand what I'm missing. I'm using
the following test class:

import java.io.*;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) throws Exception {
if (args == null || args.length != 1) {
System.out.println("Specify filename.");
System.exit(1);
}

File input = new File(args[0]);
InputStream is = new FileInputStream(input);

int lineNum=0;
for (Scanner s = new Scanner(is); s.hasNextLine();) {
String line = s.nextLine();
System.out.println(lineNum++ + " " + line);
}
}
}

And input file with no trailing newline created by:
$ cat > testfile
lineone
linetwo^d^d

The following happens:

$ java -cp . ScannerTest
0 lineone
1 linetwo

This is what I'd expect. What's it do for you?

You're not missing anything, your code is subtly different. I figured
out the problem.

In my code, I had:

for ( String ln=in.nextLine() ; in.hasNextLine() ; ln=in.nextLine() )

which works fantastically until it gets to the last line in the file.
The "increment" part of the for loop (ie. ln=in.nextLine()) goes and
gets the last line. Then the second part of the for loop checks
(in.hasNextLine()) and returns false - then it skips the body of the
loop.

This caused the following problems:
1) I missed the last line of input because the loop exited too early.
2) When I tried adding an ln=in.nextLine() statement after the for
loop, to gather the "missing" last line, I triggered a
NoSuchElementException because, clearly, I had already loaded the last
line in the file.

The solution is to make sure that the test is done BEFORE I read in
the next line. So the following code works perfectly:

while(in.hasNextLine()){
ln=in.nextLine()
}

Thanks to everyone for the help.
/DemonWasp

.



Relevant Pages

  • Re: Missing I-265 Section/Bridge over Ohio River
    ... missing section of beltway around Louisville. ... Kentucky is moving forward with plans to build a bridge there and complete the ... what of the west quadrant of the loop (I-64 in New ... The western loop, which isn't "missing" by ...
    (misc.transport.road)
  • Re: problem creating string from list
    ... : You may be missing that what's happening can be written as: ... I also ran into an interesting scoping problem whilst playing with this. ... has $var limited to the scope of the loop. ... I ran into the unexpected situation where I had an unexpectedly short scope ...
    (comp.lang.perl.misc)
  • Re: File test
    ... I thought I was missing a better approach! ... I also have an error counter and a sleep in the loop so as not to wait for ... writing is slow as its over a WAN!) ... so I want to loop until the file is ...
    (microsoft.public.dotnet.languages.vb)
  • Re: PL/SQL - Processing arrays where an element may be null
    ... end loop; ... Another way to handle this would be to wrap update into an exception ... detect and handle missing collection elements issue (for example, ...
    (comp.databases.oracle.server)