Ask coding questions

← Back to all posts
Typewriter Effect
poetaetoes (326)

How do I have a typewriter effect in Java?

Answered by JBloves27 (1887) [earned 5 cycles]
View Answer
Comments
hotnewtop
JBloves27 (1887)

You could do something like this:

String text = "hello world";
int i;
for(i = 0; i < text.length(); i++){
    System.out.printf("%c", text.charAt(i));
    try{
        Thread.sleep(500);//0.5s pause between characters
    }catch(InterruptedException ex){
        Thread.currentThread().interrupt();
    }
}

However, it might lack a bit of consistency because you have to copy and paste that over and over.
Hope this helps!

codingjlu (226)

Well, if you don't handle it, Java's gonna get mad at you for the unhandled InterruptedException exception. Here's a working TypeWriter effect in Java (a repl I created a while ago): https://replit.com/@codingjlu/Slow-Typer