aspose file tools*
The moose likes Beginning Java and the fly likes Program keeps locking up, not sure why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program keeps locking up, not sure why" Watch "Program keeps locking up, not sure why" New topic
Author

Program keeps locking up, not sure why

Donnie Inman
Greenhorn

Joined: Mar 21, 2011
Posts: 2
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.

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


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.


[My Blog]
All roads lead to JavaRanch
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210



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

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

What mistake ?
Donnie Inman
Greenhorn

Joined: Mar 21, 2011
Posts: 2
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program keeps locking up, not sure why