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...