• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

If ... else ?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 64668
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    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
    Marshal
    Posts: 64668
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!