Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For loop error?

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code but it never gets to

i have checked the values for variables and they all seem to be correct. The method getMatrix is defined in another class but I'm still having having problems. I've tried going through the for loops myself and can't see anything wrong with them. Can you understand why?


[ EJFH Edited formatting]
[ March 10, 2004: Message edited by: Ernest Friedman-Hill ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've reformatted your code so that the indentation shows the structure. It looks like you're editing code in some sort of editor that doesn't do this for you automatically -- you really should consider using a better editor or IDE; a real programmer's editor is a huge help. An automated formatter can instantly show you problems (like the problem here!) with your code.
In any case, with the indentation above, it's easy to see that the small loops over "i" and "all" near the bottom of the page are actually inside the large loops over "i" and "all" that start near the top of the routine. In all the time I've been programming and teaching, I've never actually seen anyone make this particular error, so congratulations
The inner loops are making changes to the loop counters being used by the outer loops. Therefore, depending on the values of the various constants, the outer loops will be executed either fewer or more times than you'd expect. Since you're saying it never gets to the bottom of the routine, I suspect that row1 is less than theRowCount, so that the variable "all" never gets large enough to finish that large inner for loop!
Anyway, I have no idea what the logic of this routine is supposed to be, but hopefully you'll be able to sort it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic