This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes If ... else ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If ... else ?" Watch "If ... else ?" New topic
Author

If ... else ?

podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
The following code checks if the notepad is open (windows xp) .. if is open it prints a msg who tell the user "notepad is open" -- but now i have to implement a message who prints if the notepad is not open, the message will be "notepad is close".

I have tryed for hours to do that lol , i tried with "else", but with no luck, i'm a n00b with java, so i hope a little push here ...

cheers ! and thanks



[ May 12, 2008: Message edited by: podonga poron ]
[ May 12, 2008: Message edited by: podonga poron ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Welcome to the Ranch.
The problem is that you are trying to combine a while loop and an if block. That works until you add an else to it.

Suggest:
  • Set up a boolean variable notepadOpen, and initialise it to false.
  • If you find "notepad" in your search of the processes, set notepadOpen to true.
  • Include !notepadOpen as an additional test for your while-loop continuation.
  • Move your if-else after the end of the while loop.
  • See whether that lot helps
    podonga poron
    Ranch Hand

    Joined: May 12, 2008
    Posts: 55


    Awright matey !

    now it works !!

    can you add your implementations too ? just to see the code in a right way, for learning you know, thanks
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38033
        
      22
    Apart from the inappropriate name for the class, and the inappropriate use of the main method to hold "real" code, you can probably lose quite a lot of it.

    Never say if (myBoolean == false); say if (!myBoolean). And similarly never say if (myBoolean == true).

    Suggest you can reduce your while loop to thisThen close the reader and write the if-else to print the result later.
     
    wood burning stoves
     
    subject: If ... else ?
     
    Similar Threads
    Regex
    Mac Address
    User Prompt Timer
    Retrieving a value ..
    OO doubt