aspose file tools*
The moose likes Beginning Java and the fly likes While loop never stops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While loop never stops" Watch "While loop never stops" New topic
Author

While loop never stops

Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43
Hi.

I have a problem. I have a jFrameForm and a GUIController class wich controlls more frames, but the first is that jFrameForm.

jFrameCode:



then i have in the GUIController class


the problem is the loop never stops, it never reach to the line with System.out.println(ag.getOk()); and i don't know why, it never change the value when i press the button. I also had put into while a print statement to verify what value get ok while(ag.getOk()!=1){ System.out.println(ag.getOk()) } and it is always 0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Welcome to JavaRanch

Your code was difficult to read; I have added CODE tags and you can see how much easier it is to read.
You are returning 0 as default from OK (By the way: that is not at all good style: 0 = false and 1 = true have been [virtually] expunged from Java), and never setting OK to anything else and not doing anything in the loop body . . .

So you will get an infinite loop. Why have you got that loop in the first place?
Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43
Yes, by default the value is 0 but when i click the button, the ok will be set to 1, so normally the while should stop. Am i not right?

I used the loop because i have to wait untill the button is pressed. When ok is 1 the button is pressed.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Have you put any print statements into setOk() to see if the method is even being called when you press the button?

SCJA
~Currently preparing for SCJP6
Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43
Yes... i did that. The method is called, ok is set to 1 but still the loop don't stop.

Also if i do a print for ok in the while loop i will have only
0
0
0
0
0
0
0
0
0
, even if i called the method setOk.
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
did it compile....can you take the pain of posting of posting the complete implementation....
Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43



Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43
i translated the code into English.... where you see Angajat that means Employee .

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I don't have a compiler or JVM here at work and I am still reading through your code but so far I noticed you are missing the close bracket for your initComponents method and the close bracket for your AngajatGUI class. I'll let you know what else I find. I have a suspicion it is due to the "this" command. You are most likely creating 2 seperate AngajatGUI objects and are getting the wrong "ok" value, or something like that.

...still looking...
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Found it. Your GUIController class is creating a new EmployeeGUI object which has it's own "ok" field. You are also creating a second one in your main method.

I would change your main method to have this in it:



Then inside GUIController I would still create the EmployeeGUI eg and then just set it to visible and keep the rest of the code the same. I don't have a JVM to test it but that should do what you'd like. There is a bit of a logic mixup in your code. Also, you never use jButton2 ;)

Hope that helps.
Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43
Trank you Brian. I understood what you said and i did some changes in my code.

I commented the new new AngajatGUI().setVisible(true); , and i added eg.setVisible(true); in My GUIController.
Now it works as i wanted. thank you again.

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
No problem, glad it worked out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: While loop never stops