File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How can we ensure that a while loop would not result in an infinite loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can we ensure that a while loop would not result in an infinite loop?" Watch "How can we ensure that a while loop would not result in an infinite loop?" New topic
Author

How can we ensure that a while loop would not result in an infinite loop?

dev deba
Greenhorn

Joined: Aug 02, 2012
Posts: 2
This is the method where I am using two while loops. I suspect an infinite loop, especially for the first one.




Please help.
Viktor Kubinec
Ranch Hand

Joined: Jan 28, 2012
Posts: 34
Hi, please edit your post and wrap the java code into code tags. This way it is unreadable and people in here are less willing to read it.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Dev Deba,

Welcome to JavaRanch

As Viktor noted, code tags makes your code more easy to read. Please read the link - UseCodeTags

I have edited the post for you this time. Please take note of it henceforth.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39782
    
  28
That still needs a lot of tidying up. I got rid of some of the longer lines and blank lines, but your incorrect indentation makes the code hard to read. There are all sorts of things you could do. You could lose the lastRecord variable, for example, which you appear never to use. I know you have written || lastRecord) but that appears always to be false, and || false is a unit, akin to + 0 or × 1, so you are doing nothing with it.
You can probably also tidy up that list of declared Exceptions.

Re‑post the code with indentation, 4 spaces at a time (you are right to use spaces, but not 8 a level).
Make sure every line after a { is indented.
Don’t write } and { on the same line. Follow every { and every } with a new line.
Get rid of complicated assignments. I suspect (but may be mistaken) that you can reducetoFfind a way to get rid of the continue; statements; simply surround the remainder of the loop in an if (...){...} with {}.
When you have got that loop down to a nice simple loop, you will probably be able to see how it works. Put some print statements inside it, so you can follow the execution on screen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we ensure that a while loop would not result in an infinite loop?