Results 1 to 17 of 17
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default crazy flags and do while loop

    I like programing in java and do it as a hobby,
    but there are certain things I dislike and which drive my crazy!
    See this eg.
    -a button activates the do/ while group
    -and the rand selects a study room out of 100 rooms
    -after a room pops up, the flag for that room goes false
    and the button gets disabled (which it does)

    But than that room should not be available anymore
    which is not true it gets selected again and again..
    wow what is wrong here, these things drive me nuts!


    Java Code:
    do {
                rand = randGen.nextInt(MAXROOM); // non piano rooms
          } while (//roominfo[rand].getPiano() &&
                  (roominfo[rand].getFreeRoom()));
          
    
          roominfo[rand].setFreeRoom(false);
          roominfo[rand].getRoomButton().setEnabled(false);
    
    
    
     public class RoomInfo  {
           private int roomnumber;
           private JButton roombutton;
           private String studentname;
           private boolean piano, roomfree;
           private long endtime;
    
           public void setRoomNumber(int rm)  {
               roomnumber = rm;
           }
           public int getRoomNumber()  {
               return roomnumber;
           }
    
           public void setRoomButton(JButton jb)  {
               roombutton = jb;
           }
           public JButton getRoomButton()  {
               return roombutton;
           }
    
            public void setStudentName(String sn)  {
               studentname = sn;
           }
           public String getStudentName()  {
               return studentname;
           }
    
            public void setPiano(Boolean pf)  {
               piano = pf;
           }
           public Boolean getPiano()  {
               return piano;
           }
    
            public void setFreeRoom(Boolean rf)  {
               roomfree = rf;
           }
           public Boolean getFreeRoom()  {
               return roomfree;
           }
    
           public void setEndTime(long et)  {
               endtime = et;
           }
           public long getEndTime()  {
               return endtime;
           }
    
       }

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    BTW the if the room has piano is even turned off
    so just checking if room is free
    There are two types of buttons:
    1) the direct select button which works well
    2) the random button which is discussed here
    Last edited by willemjav; 03-17-2013 at 02:46 PM.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: crazy flags and do while loop

    Hi willemjav,

    It looks as though your do-while loop on lines 1 to 4 is inverted.
    I'm guessing the intended functionality is to loop until it finds an available room but the current functionality causes the loop to continue until roominfo[rand].getFreeRoom() returns ''false'.

    Regards.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    yes ronin that's what I thought too
    but when I do this
    !roominfo[rand].getFreeRoom()
    I can not even get out of the loop,
    so what is going on here

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    So what it does is this: when the room is occupied it is false,
    and the rand should look for another one until finds true,
    which means a free room found!
    Than unable the button and set the flag false.
    So that room can not be occupied again!
    But it does pop again....
    I am monitoring all:

    Java Code:
    System.out.print(" rand " + rand);
          System.out.println(" randroom " + roominfo[rand].getRoomNumber()
                  + " freeroom " + roominfo[rand].getFreeRoom() +
                  " pfbutton selected " + pf +
                  " pfroom selected " + roominfo[rand].getPiano() +
                  " room index " + getRoomNumberIndex(roominfo[rand].getRoomNumber()));

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    You see eg rand 23 popping up two times, which can not be (insane)


    run:
    rand 82 randroom 522 freeroom false pfbutton selected false pfroom selected false room index 82
    rand 56 randroom 426 freeroom false pfbutton selected false pfroom selected false room index 56
    rand 50 randroom 420 freeroom false pfbutton selected false pfroom selected false room index 50
    rand 65 randroom 505 freeroom false pfbutton selected false pfroom selected false room index 65
    rand 77 randroom 517 freeroom false pfbutton selected false pfroom selected false room index 77
    rand 0 randroom 300 freeroom false pfbutton selected false pfroom selected true room index 0
    rand 23 randroom 323 freeroom false pfbutton selected false pfroom selected true room index 23
    rand 17 randroom 317 freeroom false pfbutton selected false pfroom selected true room index 17
    rand 48 randroom 418 freeroom false pfbutton selected false pfroom selected false room index 48
    rand 86 randroom 526 freeroom false pfbutton selected false pfroom selected false room index 86
    rand 33 randroom 403 freeroom false pfbutton selected false pfroom selected true room index 33
    rand 62 randroom 502 freeroom false pfbutton selected false pfroom selected true room index 62
    rand 6 randroom 306 freeroom false pfbutton selected false pfroom selected true room index 6
    rand 23 randroom 323 freeroom false pfbutton selected false pfroom selected true room index 23
    rand 20 randroom 320 freeroom false pfbutton selected false pfroom selected true room index 20

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: crazy flags and do while loop

    The default value for a boolean is false so you need to set the 'roomfree' variable to true for each room upon initialization.

    Regards.

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    Is here some paranormal quantum heap at work!!!
    please help me to install logic and sense again into our SUN cult
    please!

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    OK ronin let see you maybe right, just a moment!

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    Yes this works, it installed my faith again, logic triumpfed!
    here is what I did


    Java Code:
    public void setRoomsFree() {
             for (int i = 0; i < roominfo.length; i++)
               roominfo[i].setFreeRoom(true);
        }
    this is the checker now: !roominfo[rand].getFreeRoom()

    sorry for my stupidity.....

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    So ronin what would be going on here than

    Java Code:
     public void fastSearchRoom(boolean pf)  {
          int rand=0;
          if (pf)  do {
                rand = randGen.nextInt(MAXROOM); // piano rooms
         } while (!roominfo[rand].getPiano() &&
                 (!roominfo[rand].getFreeRoom()));
    
         else do {
                rand = randGen.nextInt(MAXROOM); // non piano rooms
          } while (roominfo[rand].getPiano() &&
                  (!roominfo[rand].getFreeRoom()));
          
          roominfo[rand].setFreeRoom(false);
          roominfo[rand].getRoomButton().setEnabled(false);
    
          System.out.print(" rand " + rand);
          System.out.println(" randroom " + roominfo[rand].getRoomNumber()
                  + " freeroom " + roominfo[rand].getFreeRoom() +
                  " pfbutton selected " + pf +
                  " pfroom selected " + roominfo[rand].getPiano() +
                  " room index " + getRoomNumberIndex(roominfo[rand].getRoomNumber()));
       }

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    when the room has a piano it is true

  13. #13
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: crazy flags and do while loop

    Let me guess, you are getting stuck in the do-while loops again.

    Depending on whether or not the room contains a piano you will get stuck in either the first or send do-while loop.
    If you want to know if a room has a piano you would be better to perform this check elsewhere instead of making it a condition in the do-while loop.

    Regards.

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    No the student either plays the piano or another instrument
    when the random buttons search a room he needs to say if
    he want one with or without piano, so he gets assigned the right room
    (and of course I have to study that or nor and stuff better)

    thanks ronin

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    this is the correct algorithm

    Java Code:
     public void fastSearchRoom(boolean pf)  {
          int rand=0;
          if (pf)  do {
                rand = randGen.nextInt(MAXROOM); // piano rooms
         } while (roominfo[rand].getPiano() ||
                  (!roominfo[rand].getFreeRoom()));
    
         else do {
                rand = randGen.nextInt(MAXROOM); // non piano rooms
          } while (!roominfo[rand].getPiano() ||
                  (!roominfo[rand].getFreeRoom()));
          
          roominfo[rand].setFreeRoom(false);
          roominfo[rand].getRoomButton().setEnabled(false);
    
          System.out.print(" rand " + rand);
          System.out.println(" randroom " + roominfo[rand].getRoomNumber()
                  + " freeroom " + roominfo[rand].getFreeRoom() +
                  " pfbutton selected " + pf +
                  " pfroom selected " + roominfo[rand].getPiano() +
                  " room index " + getRoomNumberIndex(roominfo[rand].getRoomNumber()));
       }
    Last edited by willemjav; 03-17-2013 at 04:12 PM.

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: crazy flags and do while loop

    Have to avoid "no room available" since
    one can not get out of the loop, hahahaha

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

Similar Threads

  1. serializable and externalizable flags conflict
    By anoorally in forum New To Java
    Replies: 0
    Last Post: 03-16-2012, 05:00 PM
  2. Flags?
    By radgator in forum New To Java
    Replies: 3
    Last Post: 05-25-2011, 01:00 AM
  3. java programming adding flags
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 12:59 AM
  4. Returning flags from enums
    By willemien in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 08:37 AM
  5. stopping thread...using flags
    By rstepler in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 10:36 PM

Posting Permissions

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