Q:

How to erase the last multi-line console output in Java

I have seen how to erase single line outputs in the console, but couldn't find a solution for multi-line outputs. How can I do this? For example,

dog
cat
fight

these three lines were the last outputs in the console and I want them erased before printing the next output.

A:

You can use linked list for this. I have already mentioned helpful resource which will help you.
import java.util.*;

class Main {
  public static void main(String[] args) {
    // Creating object of class linked list 
        LinkedList<String> object = new LinkedList<String>(); 

        // Adding elements to the linked list 
        object.add("John"); 
        object.add("dog"); 
        object.add("cat"); 
        object.add("fight"); 
        object.add("Stop"); 
        System.out.println("Linked list : " + object); 

        // Removing elements from the linked list 
        object.remove("dog"); 
        object.remove("cat"); 
        object.remove("fight");
        System.out.println("Linked list after deletion: " + object); 
  }
}

Output -:
Linked list : [John, fight, dog, cat, Stop]
Linked list after deletion: [John, Stop]

If you want to remove your desired words before printing next word, you can try this.
LinkedList<String> object = new LinkedList<String>(); 

// Adding elements to the linked list 
object.add("John"); 
object.add("dog"); 
object.add("cat"); 
object.add("fight"); 

//Removing desired words
object.remove("dog"); 
object.remove("cat"); 
object.remove("fight");

//Next words
object.add("Stop"); 

System.out.println("Linked list : " + object);

Output -:
Linked list : [John, Stop]

