Re: Scanner class and last line in a file




"DemonWasp" <jda980@xxxxxxx> wrote in message news:r5pdh2dvsta11t72r80ffabmrln33ffrmp@xxxxxxxxxx
I've been having some trouble getting the Scanner class to operate the
way I'd like. I'm doing some fairly basic file IO and I can't seem to
get the class to load the last line/token any way I try.


First, I tried using the following:

File InFile=new File("input.txt");
Scanner In=new Scanner(InFile);

String Ln;
for ( Ln=In.nextLine() ; In.hasNextLine() ; Ln=In.nextLine() ) {
// Do stuff with Ln in here.
}

Unfortunately, this moves the "cursor" in the file down one line, and
returns the text skipped - probably not what I was trying to do. Also,
when it gets to the end of the file, hasNextLine() returns false (as
it should) and the loop finishes. Unfortunately, it's still left
precious information in the file!

When I tell it to load the final line using nextLine(), it tries
advancing past the end of the file and gives me a
NoSuchElementException.

In your "Do stuff with Ln in here" section, doesn't it process the last line right before exiting the loop?



So, I tried changing it up to be the following:

File InFile=new File("input.txt");
Scanner In=new Scanner(InFile).useDelimiter("\n");

Don't bother with "useDelimiter". I think that'll be a dead end for you.

- Oliver

.



Relevant Pages

  • Re: UC3843 power supply help needed!!!
    ... >> I have built a power supply using a uc3843 chip and need some help. ... >> I found by checking the drive to the FET that the loop seems unstable ... >> at certain loads.As I increace the load from 0A to 2.5A ... >> go about fixing it.I have tried changing the gain resistorto ...
    (sci.electronics.design)
  • Re: UC3843 power supply help needed!!!
    ... I have built a power supply using a uc3843 chip and need some help. ... I found by checking the drive to the FET that the loop seems unstable ... at certain loads.As I increace the load from 0A to 2.5A ... with opto feed back you need to acount for the wide variation in loop gain ...
    (sci.electronics.design)
  • Re: UC3843 power supply help needed!!!
    ... > I have built a power supply using a uc3843 chip and need some help. ... > I found by checking the drive to the FET that the loop seems unstable ... > at certain loads.As I increace the load from 0A to 2.5A ... > go about fixing it.I have tried changing the gain resistorto ...
    (sci.electronics.design)
  • Too many objects loaded at the same time and cause OutOfMemory exception.
    ... objects of grade, class, student, lesson, semester, now let's say i ... each student has 5 lessons. ... all students's score in fall of 2006, i have to first load grade ... loop into grade collection; ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Delay in calls to stored procedure
    ... load under burst? ... (sounds like burst is the issue here) ... > I'm running through a loop calling a stored procedure each time. ... > The application is being run on the database server and I'm using the IP ...
    (microsoft.public.dotnet.framework.adonet)