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,610
  • 2
  • 26
  • 32
Mahtab
  • 269
  • 3
  • 11

3 Answers3

1

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

System.out.print(arr[i]+"\r")
NiziL
  • 5,068
  • 23
  • 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,664
  • 1
  • 29
  • 45
0

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

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
talex
  • 17,973
  • 3
  • 29
  • 66