clear console

adroit89's Avatar
Go4Expert Member
can any one help me how to clear the console in java???
if u can please provide me the code.
Thank you.
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
[ Nice question, I wonder why is it unanswered yet . ]

In Java, it can be done in 2 ways :

(1) STUPID WAY
Code: java
import java.lang.*;

public class Main {
        public void ClearConsole() {
                for (int i = 0; i < 25; i++)
                        System.out.println("\n");
        }

        public static void main(String[] args) {
                new Main().ClearConsole();
          }
}


(2) SMART WAY
Code: java
import java.lang.*;
import java.io.Console;

public class Main {
        public void ClearConsole() {
                Console console = System.console();       
                if (console == null)
                        System.out.println("Couldn't get Console object !");
                console.clear();
        }
   
        public static void main(String[] args) {
                new Main().ClearConsole();
        }
}
0
devunion's Avatar, Join Date: Sep 2008
Go4Expert Member
I think that it has sense to combine these two methods.
0
onzo's Avatar, Join Date: Oct 2010
Newbie Member
hi
im trying to make clear (console.clear()) but it says that its not defined for class console
i write the same function u wrote and same imports
0
teebs's Avatar, Join Date: Feb 2012
Newbie Member
Pls all am new here... And am also knew to programming, but i like it. Pls help me