File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to terminate a loop when the user presses a key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to terminate a loop when the user presses a key?" Watch "How to terminate a loop when the user presses a key?" New topic

How to terminate a loop when the user presses a key?

Howard Ting

Joined: Feb 17, 2004
Posts: 19
I wrote a method to simulate a typewriter as follows:

public static void typewrite(String s,long interval) {
String[] words = s.split("\\s");
int len = 0;
for (int j = 0;j < words.length;j++) {
len += words[j].length() + 1;
for (int i = 0;i < words[j].length();i++) {
try {Thread.sleep(interval);}
catch(InterruptedException e) {}
try {if (words[j].endsWith(".") || words[j].endsWith(",") || words[j].endsWith("!") || words[j].endsWith("?")) Thread.sleep(interval * 3);}
catch(InterruptedException e) {}
try {
if (len + words[j + 1].length() > 80) {
len = 0;
else System.out.print(" ");
catch(ArrayIndexOutOfBoundsException e) {}

This method is to introduce something, so there should be a mechanism of skipping the introduction if the user doesn't want to read it.
The case is different from terminating a common loop because we can't stop the loop whenever only one character is shown and ask "Do you want to skip this introduction?"! We should let the user to skip it by just pressing one key.
And of course, this program is run in the console mode, so the KeyListener is unavailable.
Could anyone give me some advices?

p.s. This is probably another getch() or kbhit() (in turbo c++) question without answers. If there's just no solution, I'll resort to letting the user to choose if he or she wants to read that introduction...
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
just a thought... could you make an frame with false visibility, requestFocus() on it, and use that as your listener?

I've heard it takes forever to grow a woman from the ground
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Well, if it's OK that that one key has to be "Enter", then you could just readLine on and interrupt the intro thread if you read anything.

[Jess in Action][AskingGoodQuestions]
Howard Ting

Joined: Feb 17, 2004
Posts: 19
Thank you two. I've tried both of your suggestions, and both of them do work, yet there're still some problems. Here are my problems and some feedbacks.

In George's way, firstly I should mention, an invisible frame cannot use requestFocus() and handle events. But when it's visible, this way goes very well. The only problem is hwo to hide this frame since I don't want the user to know about it.
I tried toBack(), but it caused the frame to lose focus. Maybe I can minimize it to the taskbar, but I don't know the exact steps.

In Friedman-Hill's way, there's a big problem - the user must terminate the introduction. If not, the input stream will halt until next input. I created a new thread to accept the user's input. I don't know if it was the thread or the input stream or even both of them didn't stop. As I said above all these procedures are done in the method typewrite() (that new thread is also created here), so everything should be destroied after typewrite() is ended. Is that correct?
It seems not, because the input stream is still waiting. I tried the method close(), but it resulted in a bigger problems - every stream was closed! That is, the input streams afterward were closed once an input stream was closed, so my program was over! How can I solve this problem?

I'm not sure if I described well. I'll post my code if somebody needs it.
I agree. Here's the link:
subject: How to terminate a loop when the user presses a key?
It's not a secret anymore!