*
The moose likes Beginning Java and the fly likes How to pause a program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to pause a program?" Watch "How to pause a program?" New topic
Author

How to pause a program?

Kacper Szmigiel
Greenhorn

Joined: Sep 18, 2012
Posts: 24
Hi! I've got a problem with my program. I want to do something like in this scheme:



Or like this:



What is solve for this? What should I import and use? Halp!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2248
    
  47
Pausing is simple, you can call the Thread class' sleep() method and specify how many milliseconds you want to sleep for.
The technique for waiting for user input depends on whether your program is CLI or GUI.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5


That's not going to work. That part in the parentheses of while loop has to be logical expression.

If you want an infinite loop (for whatever reason), you might as well put:


For pausing for 1 second, you can use


The quieter you are, the more you are able to hear.
Kacper Szmigiel
Greenhorn

Joined: Sep 18, 2012
Posts: 24
"Thread.sleep called in infine loop." What's wrong? It's by Kemal's method.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your while loop is wrong, it won't compile.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4180
    
  21

Kacper Szmigiel wrote:"Thread.sleep called in infine loop." What's wrong? It's by Kemal's method.


Technically nothing wrong with it... but you should have a graceful way for users to get out of it.

If you are using a console application (rather than a GUI) then your strategy for:
"wait until user will click any key"

should be to Print a prompt to System.out with a message like 'Press Enter to Continue' then read a line from System.in. There are a bunch of ways to do this, so pick your favorite method (wrap System.in into a BufferedReader, wrap it in a Scanner, use a java.io.Console etc...)

If you are using a GUI to do this, then your through process is backwards. Instead of asking:
"wait until user will click any key"

You should be asking
"make this code run when user will click any key"

And the way to solve that is a Listener of the appropriate type (ActionListener, KeyListener depending on what click and key means), and in the appropriate place (a window, field, panel, or whatever).


Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

Kemal Sokolovic wrote:
That's not going to work. That part in the parentheses of while loop has to be logical expression.

you could do this:


Do you see/understand the difference? Note that this is still considered bad form, but it would work.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5

// this works but smells for a language lacking a genuine boolean type
while (1 == 1) {
...
}

// this reflects the intention of the programmer somewhat clearer
while(true) {
...
}
Kacper Szmigiel
Greenhorn

Joined: Sep 18, 2012
Posts: 24
Thank you all for answers. It was important to me 'cause of my Fibonacci Numbers program. I've made it by this:



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pause a program?