• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't Find the Problem  RSS feed

 
Heidi Atwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been staring at this code for an hour and I can't figure out why it's hanging up. The while loop never happens. I'm sure it's horribly obvious but if someone could point out the problem I'd be grateful or at least give me a hint. The result is supposed to be: a-b c-d

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heidi Atwood wrote:I've been staring at this code for an hour and I can't figure out why it's hanging up. The while loop never happens. I'm sure it's horribly obvious but if someone could point out the problem I'd be grateful or at least give me a hint. The result is supposed to be: a-b c-d


Generally, it is a good idea to have the parens correctly indented. If something feels off -- such as there seems to be a missing parens in the while loop -- perhaps it is a good idea to correct the indentation.

A 10 second check, and correct of the indentation, yields this...



Henry
 
Heidi Atwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first off, thanks for the superspeedy response! Second, when I try to run your code, it hangs up just like mine. When I compare line by line I'm still missing the difference - sorry if my formatting was wonky - I was using the default (or thought I did) shift-control F in Eclipse to get everything lined up correctly.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a good example of the benefits of formatting your code properly and consistently.
Had you done so you I sure you would have noticed that your x = x-1 is outside of the while loop.

Another useful tip is if a loop is executing the wrong number of times, add a print statement outputting the value of the variables used in the loop test so you can see why the test is never failing.

Edit: Doh!! too late again.
 
Heidi Atwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh man!!! Talk about obvious mistakes and still not getting a clue - thank you, Henry, for gently trying to get me to see it and thanks Tony, for making sure I saw it.

Heidi
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something else that is simple that often works is to put System.out.println() statements everywhere. If you don't think your loop is running, stick it in as the first statement inside the loop. put another at the end, and print out your sentinal to make sure it changing. It is a quick, easy way to see what is happening in your code.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!