Results 1 to 12 of 12
  1. #1
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default while and proper conditions for if

    Hello all:)

    I'm trying to write script which change var depending on other var value:
    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;
    }
    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.

    /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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    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?

  3. #3
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    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());

  5. #5
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    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:
    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;
    		}
    In some moment I see:
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!Otwieram kase 3!!!!!!!!!!!!!!!!!!!
    I dont want stop loop but use every if just once.
    I know I can figure it out by myself but thanks for any advices.

  6. #6
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Ok, I've got it, I added new int with number which determine which cashbox should be opened.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    they haven't got proper conds for end repeating.
    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.

    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Can I recommend you use proper indenting for your code as well?

  9. #9
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Yes, please, couse I'm still having problems with this. Now i'm working with:
    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;
    		}
    		}
    Bank.LICZNIK - number of active clients. I want to open next cashboxes depending on this number.
    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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Honestly, indent your code!

  11. #11
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Like this?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    That's better.
    Can actually see the flow now, rather than simply a slab of stuff.

Similar Threads

  1. trouble validating 2 conditions
    By cottoneye1256 in forum New To Java
    Replies: 2
    Last Post: 08-23-2010, 06:53 PM
  2. proper use of IllegalArgumentException
    By vendetta in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 07:43 PM
  3. pls expalin me with any proper example
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-05-2009, 10:35 AM
  4. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 05:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •