1

So I'm learning how to code Java and decided to start with a story-like game. I have a method/function that slows down the dialogue printing so that it looks like typing. However, I can't use scanners to detect next line and stop the code because the scanner isn't constantly active. How would I code this? I know it has something to do with key listeners but I still couldn't figure it out.

Here is the code I'm trying to do:

for(int o = 0; o < diatext.length(); o++){
  if(KeyEvent.VK_ENTER == 1) {
    speed = 0;
  }
  Thread.sleep(60);//0.06s pause between characters
  System.out.printf("%c", diatext.charAt(o));
}
cr0w
  • 19
  • 3
  • java has a printf? when did that happen? Also, you can try something like this `Scanner scanner = new Scanner(file); while (scanner.hasNext()) { System.out.println(scanner.next()); } scanner.close();` put an if statement looking for enter. Its nice to hear someone getting interesting in code around here! Welcome to stack overflow! – washcloth Mar 21 '20 at 05:59
  • @Washcloth Since 1.5. [PrintStream](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/PrintStream.html#printf(java.lang.String,java.lang.Object...)) – akuzminykh Mar 21 '20 at 06:04
  • unfortunately, ive already tried the scanner. While it does work, it only works after the thread has finished its typing, which is really weird. My plan was to use something that can immediately stop the code. thanks for the welcome :) – cr0w Mar 21 '20 at 06:05
  • Can the printing run on a separate thread that waits for a stop signal? So long as the printing is not stuck on a infinite loop it should be interrupted. – fpezzini Mar 21 '20 at 07:06

0 Answers0