• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
dev deba
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48935
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic