wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How Come My Reader Is Skipping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Come My Reader Is Skipping" Watch "How Come My Reader Is Skipping" New topic
Author

How Come My Reader Is Skipping

Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

I have a test .txt file that looks as follows:

DataImport
148
Global
148
Global



When I run this code, I get the following:

Raw in Loop is: 148
Raw in Loop is: 148
Raw in Loop is null

What is happening to DataImport and every other line ("Global") of the file I read in?

Thanks.

Al
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

In your while-clause you read the next line, to see if it's there. But you ignore it -- you don't assign it to a variable. Then in the body of the loop, you read another line.

The standard idiom for reading a text file one line at a time is this:


I would suggest you use that, for two reasons: (1) It works (2) It's commonly used, so other programmers will be more likely to recognize what it does.
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Duh... figured it out:



It still gives me some funny encoding in the string (which I'm now putting in a StringBuilder) at the beginning of the file. But, I can work around that.
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Thanks Paul. I'll make that change.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
You are still better putting the assignment into a while loop.But why, after Java5, are you not using a Scanner?
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Thanks Campbell. I did change the while loop yesterday. I am not using a Scanner because I did not know about that object. This was supposed to be a simple parser to feed a database for an ancillary process that needs to run on demand (rarely). But, after reading about Scanner, I can certainly see how it can be very useful. Rather than writing:



Now I can write:

int intVal = Scanner.nextInt();

I will change my object. Thanks for your help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Not only can you use a Scanner like that, but you can also, if necessary, precede that code withThat can reduce the Exception handling no end.

But beware, Scanners they have peculiar behaviour with line ends; you may occasionally need to call nextLine() and discard the result to get back in step with the entries.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Campbell Ritchie wrote:Not only can you use a Scanner like that, but you can also, if necessary, precede that code withThat can reduce the Exception handling no end.

But beware, Scanners they have peculiar behaviour with line ends; you may occasionally need to call nextLine() and discard the result to get back in step with the entries.

In fact, your loop will never end once started. If the next element is not an integer, you should still "eat" it:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

In my case, I still have to test. I gave my guys a very simple way to get me data. So that they can load anytime they like. They just have to give me an int value followed by all the string values they want and then on the next int value (or at the end of all string values for the last int value), the system feeds the db and collects all the next string values associated with that int value. Make sense?

So, in my code, I would:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Thank you Rob, for the correction
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Come My Reader Is Skipping
 
Similar Threads
Passing a file to a method
How to find end of file in java
StringTokenizer / NullPointerException problem
fix reading only the last line of the file?
BufferedReader problem