alternative ways of writing these lines...

hello

I have couple of codes, very simple, but I would like to learn other ways to write instead of classis ways which what i have wrote. : )



public void istalking(int data) {

private int setcomm = 0;
 private static String IsSpeakerOn = "no";                             
     private static String IsListenerOn = "no";      //          
                while(IsSpeakerOn == "yes"){ExtraSpeaker.sleep(); }                
                          if (IsListenerOn =="yes")
                      {
                                  IsSpeakerOn ="yes";                                  
                                  setcomm = word;                                 
                                  currentcondition.wake();                  
                      }
                            else
                                        {
                                              IsSpeakerOn ="yes";             
                                              setcomm = word;                      
                                              currentcondition.sleep();            
                                              IsSpeakerOn ="no";                  
                                              IsListenerOn ="no";
                          }
               }
anosTekbotAsked:
Who is Participating?
 
objectsCommented:
the difference between a while and a do while loop is when the test is done.
in a do it is done at the start of the loop,
in a do while it is done at the end of loop meaning the loop itself will always get executed at least onse. this would not seem appropriate in your case.
0
 
objectsCommented:
thats not valid java code, what exactly is it you are trying to achieve?
0
 
MogalManicCommented:
The only obvious change I can see is replacing the strings with Booleans:
  public void istalking(int data)
    {
        int setcomm = 0;
            int word = 0;
        boolean IsSpeakerOn = false;
        boolean IsListenerOn = false; //        

        while (IsSpeakerOn == true) {
            ExtraSpeaker.sleep();
        }

        if (IsListenerOn == true) {
            IsSpeakerOn = true;
            setcomm = word;
            currentcondition.wake();
        } else {
            IsSpeakerOn = true;
            setcomm = word;
            currentcondition.sleep();
            IsSpeakerOn = false;
            IsListenerOn = false;
        }
    }

What are you trying to accomplish?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
anosTekbotAuthor Commented:
i know it is not a valid code becasue there are other parts, but my concern about the while loop and if statements. other than using booleans ..
my question is there a way these line of  statements
0
 
objectsCommented:
what is your concern?
0
 
anosTekbotAuthor Commented:
to know more about loops and statements..
for example
i tried to use
do {
...........
} while (IsSpeakerOn == "yes");

but when i tested .. it will take longer time than average and cost the machine to terminate ..
0
 
objectsCommented:
> IsSpeakerOn == "yes"

btw, that should be:

IsSpeakerOn.equals("yes")
0
 
MogalManicCommented:
Is this a multi-threaded application?

What does the WHOLE application do?
0
 
anosTekbotAuthor Commented:
i c thats why i get terminated after a do while loop
thanks object and all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.