Jonathon M
Jonathon M

Reputation: 71

Java short delay

I have been trying to create a way to recreate the pokemon's "Rolling text" where one letter appears at a time. The problem is being able to create a timer short enough to make it reasonable.

This is what i have tried:

public static void roll(String text) {
    int i = 0;
    while(i < text.length()) {
        try {
            Thread.sleep(200);                 //1000 milliseconds is one second.
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    System.out.print(text.charAt(i));
    i++;
    }
}

This will work fine if i set the value in Thread.sleep to anything above 250. The problem is, if i set seconds below 250 then it will wait the entire length of time (If i tell it to wait 100 millisends 100 times, it will wait 10 seconds) before outputting anything.

Im not sure if this is a problem with the type of timer itself or if there is something else at play here.

Upvotes: 3

Views: 277

Answers (1)

Kayaman
Kayaman

Reputation: 73548

You can use System.out.flush(); to force the buffer to be written out.

Upvotes: 3

Related Questions