Re: Scanner class and last line in a file



DemonWasp wrote:
On Sun, 24 Sep 2006 17:25:34 -0400, Lew <conrad@xxxxxxxxxxx> wrote:

<snip>

Scanner.hasNextLine()
"Returns true if there is another line in the input of this scanner. ... The scanner does not advance past any input."

Scanner.nextLine()
"Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line."

In other words, nextLine() is supposed to return the text that was skipped.

I recognise this, but I probably didn't make it sufficiently clear in
my post. However, this wasn't the main issue.

If your input has only one line (or zero), the loop will not execute. The initialization will read the one line, then the condition will fail because there is no second line.

When the loop finished and you tried to call nextLine() again (assuming you used the same Scanner instance), it threw the exception because hasNextLine() had already failed. Calling nextLine() when ! hasNextLine() raises the exception.

Try something like:

Scanner in = new Scanner( inFile );
while ( in.hasNextLine() )
{
String line = in.nextLine();
// do something with line
}

(The convention is to name variables with a lower-case first letter.)

Yeah, but I'm unconventional :P. Again though, this example misses the
point of the question that I was asking.

What I *was* asking was "how do I get the last line from a file, even
when there is no trailing whitespace?"

Thanks anyway,
DemonWasp

I don't confuse being unconventional with being dense. He just showed you what you need to do. Personally, I would point you to read the Scanner api. Learn how to Google the groups also.

--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
.



Relevant Pages

  • Re: Scanner class and last line in a file
    ... "Returns true if there is another line in the input of this scanner. ... nextLine() is supposed to return the text that was skipped. ... If your input has only one line, the loop will not execute. ... When the loop finished and you tried to call nextLineagain, it threw the exception because hasNextLine() had already failed. ...
    (comp.lang.java)
  • Re: Scanner class and last line in a file
    ... "Returns true if there is another line in the input of this scanner. ... nextLine() is supposed to return the text that was skipped. ... hasNextLine() raises the exception. ...
    (comp.lang.java)
  • Re: Virus scanner *solely* for e-mail?
    ... I was using F-Prot for DOS as a ... weekly verification of what the resident on access scanner was doing. ... which described how to call F-Prot for DOS as a Mercury Mail AV Policy. ... > Subject: Policy Exception Advice ...
    (microsoft.public.security.virus)
  • Re: gleichzeitigen Dateizugriff verhindern
    ... mal eben so umstellen. ... verschiebt und darauf meine Scanner loslassen. ... Öffnen eine Exception zu bekommen? ... der konkurrierende Scanner mittendrin dieselbe Datei ...
    (de.comp.lang.delphi.misc)
  • Question on USB scanner--older model
    ... I have a 6 year old HP Scanjet 5400c scanner. ... 2005 with one exception: I can't use the scanner buttons to scan if I put ... standby, will it likely cause problems with my programs setup to use the ...
    (microsoft.public.windows.mediacenter)