Results 1 to 7 of 7
  1. #1
    famim123369 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default exiting a while loop

    I am creating a little program to do a couple mouse clicks. Everything works, but I want it to run forever, unless I press a certain key, say for instance the letter "x" or something along those lines.

    Please Help!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: exiting a while loop

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    famim123369 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: exiting a while loop

    Quote Originally Posted by DarrylBurke View Post
    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    package practice;

    public class Practice
    {
    public static void main(String[] args)
    {
    boolean logic = true;
    while(logic==true)//possibly enter something here so that when i press "x" the loop stops
    {
    System.out.println("WORKS");
    }
    }
    }

    like the comment says, what can i enter in the condition of the while loop so that it stops when i press a designated button on the keyboard (can i even do that?)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: exiting a while loop

    Since this isn't a GUI program but rather a console program, you can't listen for key strokes and respond to them as you would in a GUI program. Instead you'll have to read in Strings, either with a Scanner object or a BufferedReader, after the enter key has been pressed, and then analyze the String for the sub-String or char of interest. Then if found, change logic to false.

    As an aside, it's usually cleaner to not have == true or == false in your while condition as that's implied. So for instance, instead of
    Java Code:
    while (logic == true) {
       //...
    }
    simply do:

    Java Code:
    while (logic) {
       //...
    }

  5. #5
    famim123369 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: exiting a while loop

    After looking into Scanner and BufferReader, I have yet another question. When I use this program, I want it to print works on most occasion, except when I press the designated button. However, the Scanner and BufferReader waits for me to input a data, I want it to wait maybe for half a second and then move on. Would i be able to do that?

    package practice;

    import java.util.Scanner;

    public class Practice
    {
    public static void main(String[] args)
    {
    Scanner a = new Scanner(System.in);
    boolean i = a.hasNext();
    System.out.println(i);
    }
    }

    So after i create the scanner object, i don't want it to stop the whole program and wait for me to enter a string. I want it to wait a short amount of time to see if i enter anything, and then move on. Please Help!

    - Thanks for all the help you have given me already!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: exiting a while loop

    It's a blocking utility, and will not move on while waiting for input. I suspect that you are looking for a GUI implementation as this would give you much more control over your input abilities.

  7. #7
    famim123369 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: exiting a while loop

    Hmm, I guess I will have to pursue a different route
    -Thanks for all the help

Similar Threads

  1. Problems with exiting application
    By JohnPringle83 in forum New To Java
    Replies: 14
    Last Post: 05-06-2011, 05:35 PM
  2. Cancel button for exiting app
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 07:20 PM
  3. Exiting a while loop using a String?
    By Isshin in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:33 PM
  4. exiting a while loop with a string
    By Aldius in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 10:38 PM
  5. Exiting on menuSelected action
    By benra in forum New To Java
    Replies: 8
    Last Post: 10-13-2009, 04:58 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
  •