clear console

Go4Expert Member
11Feb2008,18:08   #1
adroit89's Avatar
can any one help me how to clear the console in java???
if u can please provide me the code.
Thank you.
~ Б0ЯИ Τ0 С0δЭ ~
15May2009,08:33   #2
SaswatPadhi's Avatar
[ 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();
        }
}
Go4Expert Member
25May2009,21:41   #3
devunion's Avatar
I think that it has sense to combine these two methods.
Newbie Member
31Oct2010,21:11   #4
onzo's Avatar
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
Newbie Member
27Feb2012,15:06   #5
teebs's Avatar
Pls all am new here... And am also knew to programming, but i like it. Pls help me