Im trying to print out some text char by char with some delay, the problem is that it waits and waits and then prints the whole sentence out. It's like it's printing char by char to a string and then printing that string out once its finished:
public static void printWithDelay(String data, TimeUnit unit, long delay)
throws InterruptedException {
for (char ch : data.toCharArray()) {
System.out.print(ch);
unit.sleep(delay);
}
}
please help (:
Why don't you use Thread.sleep()
?
import java.lang.*;
public class PrintWithDelayExample {
public static void main(String[]args) {
printWithDelay("Hello! World", 500);
}
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();
}
}