Learn to Code via Tutorials on Repl.it!

← Back to all posts
Slow Print(In java)
EpicGamer007 (1121)

I noticed a lot of the repls in repl.it are in python and a lot of the console programs are in python(because it is the most popular). A lot of them use slow print. So this tutorial teaches you how to slow print(print 1 char, wait, then write another char) in java.

My Code:

public class Main{

  public static void main(String[] args){
    slowPrint(50, "THIS WILL BE PRINTED CHAR BY CHAR");
  }

  public void slowPrint(int time, String toPrint){
  
    char[] toPrintC = toPrint.toCharArray();// Create a char array and assign that to a string converted to a char array

    for(int i = 0; i < toPrintC.length;i++){ // Create a for loop
      System.out.print(toPrintC[i]);// Print the char
      try{
        Thread.sleep(time);// Stop printing for the amount of milliseconds specified(in the variable time)
      }catch(Exception e){
        e.printStckTrace();
      }
      // We surround in try/catch because Thread.sleep throws an exception
    }

  }

}

Hope this helps!

Commentshotnewtop
Highwayman (1439)

Does Thread.sleep always throw an exception?

EpicGamer007 (1121)

@Highwayman, yes, unfortunately it does. Good news is, I just finished the java OOP tutorial! I will post it now. (Remember how you asked why you should hide your data)

Highwayman (1439)

@AbhayBhat hm strange...

Thanks! Yay! 😋

EpicGamer007 (1121)

@Highwayman, Though about the Exception, you can dodge it by having EACH METHOD THAT USES IT throws InterruptedException but then you would have to do that for EVERY METHOD THAT USES IT(As I just said) so I do not recommend doing that

Highwayman (1439)

@AbhayBhat hm ok, seems a bit like a hack anyways lol. Ty