Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Come My Reader Is Skipping

 
Al Johnston
Ranch Hand
Posts: 99
Flex Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Flex Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Flex Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. I'll make that change.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Flex Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Al Johnston
Ranch Hand
Posts: 99
Flex Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob, for the correction
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic