1

I'm trying to make a program that makes 3 dots "..." appear one by one and then start from the beginning on the same row; something like this:

Phase 1: .
Phase 2: ..
Phase 3: ...
Phase 4: .
Phase 5: ..

and so on.

enter code here


    String text2 = "..." + "\n";
    for (int i = 0; i <= 3; i++) {

        for (int j = 0; j < text2.length(); j++) {
            System.out.print("" + text2.charAt(j));
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

        }
    }

I've tried this but it doesn't quite do it...

Pshemo
  • 122,468
  • 25
  • 185
  • 269
MickeyMoise
  • 259
  • 2
  • 13
  • Does this answer your question? [Java: Updating text in the command-line without a new line](https://stackoverflow.com/q/4573123/6395627) – Slaw Mar 20 '20 at 21:57
  • 3
    You can, but it's not guaranteed to work in all environments. – Idle_Mind Mar 20 '20 at 22:06

1 Answers1

1

You can print a backspace \b as long as the dots like so:

public static void main(String[] args)
    {
        String text2 = "...";
        for (int i = 0; i <= 3; i++) 
        {
            for (int j = 0; j < text2.length(); j++) {
                System.out.print("" + text2.charAt(j));
                try {
                    Thread.sleep(300);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
            }
            System.out.print("\b".repeat(text2.length())); //Java 11
        }
    }

Also remove the new line in your string since it will cause the dots to print on separate lines.

Bahij.Mik
  • 1,358
  • 2
  • 9
  • 22