How do I make timed (delayed) text in Java?

I’m working on a small project, how do I make the text delayed?
Repl link:

import java.util.Scanner;

public class Main {
  
    // Declaring ANSI_RESET so that we can reset the color
    public static final String ANSI_RESET = "\u001B[0m";
  
    // Declaring the color
    // Custom declaration
    public static final String ANSI_RED = "\u001B[31m";
  
    // Main driver method
    public static void main(String[] args)
    {
        // Printing the text on console prior adding
        // the desired color
        System.out.println(ANSI_RED + "This text is red");
      
      String ANSI_BLUE = "\u001B[34m";
      System.out.println(ANSI_BLUE + "This text is blue");

      String ANSI_GREEN = "\u001B[32m";
      System.out.println(ANSI_GREEN + "And even though deodorant exists...");

      String ANSI_YELLOW = "\u001B[33m";
      System.out.print(ANSI_YELLOW + "You still smell like Poo 💩");
      
    }
}```

Hey @OluwademiladeOy, I believe you can use the built-in Thread class for this:

. . .

System.out.println(ANSI_GREEN + "And even though deodorant exists...");
Thread.sleep(1000) // this is in milliseconds

String ANSI_YELLOW = "\u001B[33m"
System.out.println(ANSI_YELLOW + "You still smell bad 💩")

. . .
1 Like

I took your advice and tried to run the new code but I got error, “./Main.java:24: error: unreported exception InterruptedException; must be caught or declared to be thrown”

One method is to add throws InterruptedException to the method signature, like so:

    public static void main(String[] args) throws InterruptedException