0

Here is my method

public static void printWithDelay(String data, long delay) {
    for (char c : data.toCharArray()) {
        try {
            Thread.sleep(delay);
            System.out.print(c);
        } catch (InterruptedException e) {}
    }
    System.out.println();
}

So if i try to run the method with a given String, it will work at 300 milliseconds, but that is a bit too slow. I want it to run kinda like in the old pokemon games where it was printed fairly fast..

If i try to change it to under 300 milliseconds pr char, the output will just stand still until the whole String has been constructed, and then it will print the String..

Please help as this really annoys me /:

  • 1
    Try invoke `System.out.flush()` after printing a character. You can also do it in one statement: `System.out.append(c).flush();`. – Holger Oct 08 '15 at 17:09
  • If that doesn’t help, you likely ran into a limitation you can’t solve. The console, you are printing to, has a limitation on the visual update intervals. In this case, you can’t get better results with printing to stdout. You will have to create your own GUI if you want more control. – Holger Oct 08 '15 at 17:27
  • Ahh okay, ill just drop it then Thank you for your help (: – Daniel Winkel-Pedersen Oct 08 '15 at 17:30
  • Instead of making new questions from the same issue you would might wanted to **edit** your [Original post](http://stackoverflow.com/questions/33019295/want-to-print-text-char-by-char-for-my-textbased-game-but-it-prints-out-the-who). – Frakcool Oct 08 '15 at 21:44

2 Answers2

0

The interval we pass in Thread.sleep(long millis) method is in millis, I ran the above code in my Eclipse with Java 8, character gets printed as per the interval specified, even if I reduce the interval to 10 it printed the character one by one.

What is the interval you tried?

Saravana
  • 12,647
  • 2
  • 39
  • 57
0

You can try putting your loop inside the try { }.

    try {
        for (char c : data.toCharArray()){
             Thread.sleep(delay);
             System.out.print(c);
        }
    } catch (InterruptedException e) {}
moforogue
  • 1
  • 1