• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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++) {
System.out.print(words[j].charAt(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) {
System.out.println();
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...
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a thought... could you make an frame with false visibility, requestFocus() on it, and use that as your listener?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if it's OK that that one key has to be "Enter", then you could just readLine on System.in and interrupt the intro thread if you read anything.
 
Howard Ting
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic