aspose file tools*
The moose likes Beginning Java and the fly likes Loops using input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops using input" Watch "Loops using input" New topic
Author

Loops using input

Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Hello Everyone,

Does anyone know why this happens? I am doing a while loop to count records from an input stream. The program sees the input; there are three records in the text file that is set up. I just want to get a record count, so I have an incrementer inside of the loop that goes until a String variable (initialized as "") is != null. I have output statements set up to help me debug the issues, and the last record in the console is displayed as null, and it counts it as another record. I checked my file; I don't have any extra spaces or carriage returns or anything, so I'm OK on that end. Why does the while loop process a null record at the end? Shouldn't the loop be performed until it ISN'T null, then stop when it sees that it is?

Here is an example of the code:


As mentioned, everything is coming in and reading OK, but the loop is not stopping BEFORE the null value. How do I prevent this? Is there a way to get around it? I tried an if statement inside of the loop (if in != null then increment numRec), but it didn't work.

Thanks in advance for any help!!
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
See in your code the value of the variable "in" is not all assigned a value inside the while loop.
It has always the default value "" .

Also i feel it's not the correct way.
The code whould be some this like this.

[ March 31, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Sorry; the first line in the loop should be:
in = input.readLine();

Thanks!
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Also the code doesnt have the cuyly braces in the while loop.
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Sorry; I was going off of memory and I just got done working 13 hours so my mind is a little whacked. The curly braces are in the code that I have (I actually opened it now). But, I tried your suggestion and it worked! Thanks! But why wouldn't the other way work - why would it loop through the last time being null? Pretend all of the correct syntactical things were present. I don't get that.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
It reads the line, gets a null, then goes back to the condition in the while and checks. Voila! It is null, so the loop stops and does not read the next line.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824



I tried this code, and it worked fine. (Note the if check). I'm not sure why when you used the if check it didn't work, but try it again.

And, for the record, I prefer this format to the while ( (in=input.readLine()) != null ) format; I find this much more readable.

Try the following as well; it's a little more readable; the logic is a little bit more straightforward (no need for the extra if):

[ April 01, 2005: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Thanks Joel! This is great! I use eclipse and don't have the software right in front of me right now, but I am going to try this as soon as I get home.

As far as the if check, that is exactly what I did. I'll try it again though to see what happens. I personally am more comfortable doing it this way instead of the readLine() in the while condition because then the while condition is just doing one thing and not two, so I'm going to try this one.

Marilyn, I think the part I don't understand is that it says to perform the steps inside of the loop UNTIL it is equal to null. So if it checks it at the end AFTER it has performed everything, isn't that a hosed way of doing it? I know it's not you, but I am just trying to understand better.

Thanks everyone so much for your help!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loops using input