0

I want to know that I want to print string one after another but in a particular time interval.

Everything is fine.

I want to know that when second string print override the first one and third override the second and so on..

How can I do this?

This is my code :

public class StringTest {

  public static void main(String args[]) {

    String arr[] = { "mahtab", "hussain", "yasir", "azmi", "saif" };

    int l = arr.length;

    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i]);
      try {
        Thread.sleep(5 * 1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}
Jonathan Drapeau
  • 2,619
  • 2
  • 25
  • 32
Mahtab
  • 239
  • 3
  • 11

3 Answers3

1

You can simply do this with the carriage return \r.

System.out.print(arr[i]+"\r")
NiziL
  • 5,008
  • 21
  • 33
1

Use this

  for (int i = 0; i < arr.length; i++) {
  System.out.print(arr[i]);
  try {
    Thread.sleep(5 * 1000);
    for(int j=0;j<arr[i].length;j++){
        System.out.print("\b");
    }
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
StackFlowed
  • 6,604
  • 1
  • 28
  • 44
0

To remove the last character from the screen, you can use System.out.print("\b")

Chris Forrence
  • 9,860
  • 11
  • 47
  • 62
talex
  • 16,886
  • 2
  • 27
  • 60