Results 1 to 12 of 12
- 08-24-2010, 03:45 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
while and proper conditions for if
Hello all:)
I'm trying to write script which change var depending on other var value:
This is working with help from Thread. If kasa1.klienci.size() is more than 5 it's changing kasa2.czynne to true. Var kasa2.klienci.size() is default 0 but it's increasing with other Thread and then reach 0 again. When it happens I want to set kasa2.czynne to false and get info.Java Code:while(true){ [B]if(kasa1.klienci.size()>= 5 && kasa2.czynne == false){[/B] kasa2.czynne = true; System.out.println("!!!!!!!!!!!Otwieram kase 2"); continue; } [B]else if(kasa2.klienci.size()== 0 && kasa2.czynne== true){ //when kasa2.klienci.size()== 0 i want this for once[/B] kasa2.czynne = false; System.out.println("!!!!!!!!!!!!Zamykam kase 2"); continue; }
/edit: works good for e.g. with else if(kasa2.klienci.size()== 0 && kasa2.czynne== true && kasa1.klienci.size()== 0) but it's not exactly what I want.Last edited by Saletra; 08-24-2010 at 04:05 PM.
- 08-24-2010, 05:47 PM #2
Try debugging the code by adding println() to show the values of all the variables.
Then you can see how they are changing and then figure out how to modify your code to have it do what you want.
Can you write what you want to do in pseudo code?
- 08-24-2010, 06:48 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
endless loop:
check if kasa1.klienci.size() is more than 5 -> kasa2.czynne = true;
check if kasa2.klienci.size()== 0 ("no other clients in cashbox") -> kasa2.czynne = false;
problem is that kasa2.klienci.size() default is 0, and then clients are comming to kasa2 (via Threads), finally kasa2.klienci.size() is 0 again and has to be closed.
- 08-24-2010, 07:34 PM #4
All I can recommend is what I say before that you try to debug your code by:
Try debugging the code by adding println() to show the values of all the variables.
Then you can see how they are changing and then figure out how to modify your code to have it do what you want.
For example, add this before the start of the if:
System.out.println('k1k.size=" + kasa1.klienci.size() + ", kasa2.czynne=" + kasa2.czynne + " , k2k.size=" + kasa2.klienci.size());
- 08-24-2010, 07:45 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Thanks.
The thing is that I know exactly what values I have. Problem is that if sections are repeated and repeated becouse they haven't got proper conds for end repeating.
Now I have:
In some moment I see:Java Code:if(kasa1.klienci.size()>= 5 && kasa2.czynne == false){ kasa2.czynne = true; System.out.println("!!!!!!!!!!!!Otwieram kase 2!!!!!!!!!!!!!!!!!!!"); continue; } else if(kasa2.klienci.size()== 0 && kasa2.czynne== true && kasa1.klienci.size()== 0){ kasa2.czynne = false; System.out.println("!!!!!!!!!!!!Zamykam kase 2!!!!!!!!!!!!!!!!!!!"); continue; } else if(kasa2.klienci.size()>= 5 && kasa2.klienci.size() <10){ kasa3.czynne = true; System.out.println("!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!"); continue; }
I dont want stop loop but use every if just once.!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
I know I can figure it out by myself but thanks for any advices.
- 08-24-2010, 07:57 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Ok, I've got it, I added new int with number which determine which cashbox should be opened.
- 08-24-2010, 08:00 PM #7
One problem you have with your code is the endless loop waiting for other threads to do something. You need to change your logic to use some other logic for inter thread communications. Look at the java.util.concurrent package. Look at CountDownLatch or Semaphore for example.they haven't got proper conds for end repeating.
Your printout does NOT show the values of any of the variables. It only shows a constant message. There are 4 variables used in the loop.
- 08-25-2010, 09:18 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Can I recommend you use proper indenting for your code as well?
- 08-25-2010, 10:14 AM #9
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Yes, please, couse I'm still having problems with this. Now i'm working with:
Bank.LICZNIK - number of active clients. I want to open next cashboxes depending on this number.Java Code:if(Bank.LICZNIK== 5 && kasa2.czynne== false){ kasa2.czynne = true; System.out.println("Open cashbox 2"); continue; } else if(Bank.LICZNIK== 15 && kasa3.czynne== false){ kasa3.czynne = true; System.out.println("Open cashbox 3"); continue; } else if(Bank.LICZNIK == 40 && kasa4.czynne== false){ kasa4.czynne = true; System.out.println("Open cashbox 4"); continue; } else if(kasa2.klienci.size()== 0 && kasa2.czynne== true){ kasa2.czynne = false; System.out.println("Close cashbox 2"); continue; } else if(kasa3.klienci.size()== 0 && kasa3.czynne== true){ kasa3.czynne = false; System.out.println("Close cashbox 3"); continue; } else if(kasa4.klienci.size()== 0 && kasa4.czynne== true){ kasa4.czynne = false; System.out.println("Close cashbox 4"); continue; } }
When there are no clients at cashbox (klienci.size()== 0) it should be closed. Var klienci.size()== 0 default is 0 :/Last edited by Saletra; 08-25-2010 at 10:20 AM.
- 08-25-2010, 10:15 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Honestly, indent your code!
- 08-25-2010, 10:27 AM #11
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Like this?
- 08-25-2010, 10:37 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
trouble validating 2 conditions
By cottoneye1256 in forum New To JavaReplies: 2Last Post: 08-23-2010, 06:53 PM -
proper use of IllegalArgumentException
By vendetta in forum New To JavaReplies: 1Last Post: 01-16-2010, 07:43 PM -
pls expalin me with any proper example
By javastuden in forum New To JavaReplies: 1Last Post: 11-05-2009, 10:35 AM -
Asking for a username with conditions
By ScentOfAWookie in forum New To JavaReplies: 2Last Post: 03-20-2009, 05:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks