Computer Science Canada

Clearing the console screen?

Author:  Jonny Tight Lips [ Thu Sep 29, 2005 8:26 am ]
Post subject:  Clearing the console screen?

Just wondering if it is pssoble to clear the console screen kind like cls in turing. Any help would be great. Thx

Author:  wtd [ Thu Sep 29, 2005 11:28 am ]
Post subject: 

No, it's not.

Turing doesn't have a console screen.

You don't want to clear the screen anyway. The user knows what he or she typed. Wiping it out only serves to make the program more difficult to use.

If you want something like a piece of text centered on the screen, with whitespace all around, in Java you can either use Swing or SWT.

Author:  1of42 [ Fri Sep 30, 2005 6:18 pm ]
Post subject: 

wtd is incorrect - on Windows:

c++:

system ("cls");


will clear the console screen.

using system calls is bad practice, and will make your program non-portable, but it IS possible to do.

Author:  wtd [ Fri Sep 30, 2005 6:30 pm ]
Post subject: 

The language in question is Java. The quickets way to do this in Java would be:

code:
Runtime.getRuntime().exec("cls");

Author:  [Gandalf] [ Fri Sep 30, 2005 7:30 pm ]
Post subject: 

Would that be portable, or not?

Author:  wtd [ Fri Sep 30, 2005 7:39 pm ]
Post subject: 

[Gandalf] wrote:
Would that be portable, or not?


It would not.

It would require that the underlying system understand "cls".

Author:  1of42 [ Fri Sep 30, 2005 9:40 pm ]
Post subject: 

Well, so wtd, I'm still right - it's not my fault that I thought this was C++ (i'm tired)! I swear! Laughing

Author:  wtd [ Fri Sep 30, 2005 9:44 pm ]
Post subject: 

Yes, unfortunately you're right.

Author:  Hikaru79 [ Fri Sep 30, 2005 11:40 pm ]
Post subject: 

Of course, if you're REALLY desperate to do this, and want it to be relatively portable, there's always something like this:
Java:
import java.io.*;

public class Test{
       
        public static void main (String [] args){
                System.out.println("Hello, world!");
                try {
                        if (System.getProperty("os.name").indexOf("Windows")!=-1){
                                Runtime.getRuntime().exec("cls");
                        } else if (System.getProperty("os.name").indexOf("x")!=-1){
                                String clear_str;
                                Process clear_proc = Runtime.getRuntime().exec("/usr/bin/clear");
                                BufferedReader clear_in = new BufferedReader (new InputStreamReader(clear_proc.getInputStream()));
                                while ((clear_str = clear_in.readLine()) != null) {
                                        System.out.println(clear_str);
                                }
                        }
                } catch (IOException f){ System.out.println(System.getProperty("os.name")); }
        }
}

This is far from perfect, but it might be enough for some purposes.

Author:  wtd [ Fri Sep 30, 2005 11:43 pm ]
Post subject: 

code:
/usr/bin/clear


Should possibly be:

code:
/usr/bin/env clear


: