• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Program keeps locking up, not sure why

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Been learning Java from Ivor Horton's Beginning Java 2 book and have made it to the end of Chpt 3 . Did an exercise to generate 5 sets of 6 numbers with no duplication within a set (ie lottery numbers) and the numbers must be between 1 & 49. Wrote this program and it works sometimes, but then sometimes the program locks up. It will display the first set of numbers and then just stop with the cursor flashing. It did work fine but I changed a spacing issue on the print line and now it won't hardly go past the first set at all. Thanks in advance for any help.

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This will block if second==first, because you're only initializing second here, its values will never change after the initialization. The second loop will not change its value, so it will loop forever. Same for the other for loops.
 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The Outer "for loop" has a mistake, not sure if this is creating that problem?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike ryan wrote:
The Outer "for loop" has a mistake, not sure if this is creating that problem?


What mistake ?
 
Donnie Inman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:
This will block if second==first, because you're only initializing second here, its values will never change after the initialization. The second loop will not change its value, so it will loop forever. Same for the other for loops.



I wonder if I'm not understanding exactly what's going on in each of these loops.



The way I'm reading these is that it will create a random number for the variable second and check that it doesn't match a previous number. If it does then won't it select another random number, check again and keep going until it finds one that doesn't match the previous. Maybe I'm totally wrong in this. The book example did it with a switch, but my first idea was to do it this way and I really want to make it work. Guess I'm stubborn.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Donnie Inman wrote:If it does then won't it select another random number

No. The first part of a for loop is the initialisation step - this is only done once. the value of second will never change after this. So it either gets set to something different from the first number and immediately breaks from the loop or it gets set to the same as the first number and just sits in the loop forever checking if the first value is not equal to the second value.
You need to move the setting of 'second to the last part of the for loop - i.e. after the second ;
You should also move the test from inside the loop to the second part of the for loop i.e. between the two ;. That way you don't need to use a break which should be avoided if possible.

Although personally, I think a while loop would be better here


Also, if you're going to use a for loop, use all it's capabilities. For the outer loop, using a for loop is correct but change it to
and get rid of
 
reply
    Bookmark Topic Watch Topic
  • New Topic