Screw it. I'll just use Swing.
Clearing The Screen?Can this be done?
33 Replies - 80448 Views - Last Post: 18 April 2010 - 11:18 PM
#17
Re: Clearing The Screen?
Posted 16 July 2007 - 11:07 AM
Actualy there is a clear screen command, well i dont know if it works for Java but it works for J# wich is another form of Java :
maybe it works in Java, dont know
System.Console.Clear();
maybe it works in Java, dont know
#18
Re: Clearing The Screen?
Posted 16 July 2007 - 11:25 AM
Actually i think that one doesnt work but this one below works fine on a MAC if you remove the last line :
remove the :
String clearScreenCommand = null; if( System.getProperty( "os.name" ).startsWith( "Window" ) ) clearScreenCommand = "cls"; else clearScreenCommand = "clear"; Runtime.getRuntime().exec( clearScreenCommand );
remove the :
Runtime.getRuntime().exec( clearScreenCommand );
#19
Re: Clearing The Screen?
Posted 16 July 2007 - 12:29 PM
aime_methe, on 16 Jul, 2007 - 02:07 PM, said:
Actualy there is a clear screen command, well i dont know if it works for Java but it works for J# wich is another form of Java :
maybe it works in Java, dont know
System.Console.Clear();
maybe it works in Java, dont know
J# is not eally another form of Java - it is a transitional language meant to be used by java developers to write .NEt framework programs. Important note - J# will be retired from future versions due to declining use.
#20
Re: Clearing The Screen?
Posted 16 July 2007 - 05:30 PM
J# is a platform-specific Microsoft bastardization of Java.
This post has been edited by alcdotcom: 16 July 2007 - 05:31 PM
#21
Re: Clearing The Screen?
Posted 16 July 2007 - 07:03 PM
#22
Re: Clearing The Screen?
Posted 22 July 2007 - 08:48 AM
Quote
for (int i =0;i<400;i++)
System.out.println("");
Actually it would be faster to send a bunch of CRLF's. Perhaps:
for (int i=0;i<400;++i) {
System.out.println("\n"); }
As for the system call throwing an exception, since "cls" is a built in command, I believe that you will need to use it as:
try {
Runtime.getRuntime().exec("cmd /c cls");
} catch (IOException e) {
e.printStackTrace();
}
The reason is that there is no "cls.exe" or "cls.bat" etc. So java has no idea what process to invoke. (error=2 is "The system cannot find the file specified") -- but there is a problem with using "cmd /c cls":
import java.io.*;
public class clear {
public static void main(String args[]) throws IOException {
try {
System.out.println("This is a test...");
Runtime.getRuntime().exec("cmd /c cls");
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
Does not clear the screen!!! The reason is that the new cmd shell is run as a new process (its io are actually being routed to java) so the cls command has no effect since the new process is not attached to a window. Although I have not tried this on my linux vm I bet that the clear command will not work either since java will start up a new JVM to run the process under.I really do not know a good way to clear a console window in java. Even writing your own "cls.exe" in C would not solve the problem as java would start it up in its own environment, not the current console window.
One should note that this is not a bug in Java, but a feature. It allows java to communicate more effectively with called processes then it did in older versions (if the exec("cls") works for you, you are on an older java platform).
#23
Re: Clearing The Screen?
Posted 18 February 2009 - 07:16 AM
I came here today looking for a command to clear the screen, with the hope of making my assignment program look better.
I searched through a lot of forums and it seems that this simple question has gone unanswered for a long time.
So I took a good look at my ASCII list, and surely enough there is a mention of a code for form feed. Then I just tried using \f in Java and it is working wonders.
So if anyone still cares, try this:
I searched through a lot of forums and it seems that this simple question has gone unanswered for a long time.
So I took a good look at my ASCII list, and surely enough there is a mention of a code for form feed. Then I just tried using \f in Java and it is working wonders.
So if anyone still cares, try this:
System.out.println("\f");
#24
Re: Clearing The Screen?
Posted 18 February 2009 - 07:27 AM
Thanks for that pointer edvella... unfortunately, you are 2 years too late.
I haven't tried it... but if it will help people in the future, nice contribution!
I haven't tried it... but if it will help people in the future, nice contribution!
#25
Re: Clearing The Screen?
Posted 18 February 2009 - 07:28 AM
Quote
I came here today looking for a command to clear the screen, with the hope of making my assignment program look better.
I searched through a lot of forums and it seems that this simple question has gone unanswered for a long time.
So I took a good look at my ASCII list, and surely enough there is a mention of a code for form feed. Then I just tried using \f in Java and it is working wonders.
So if anyone still cares, try this:
CODE
System.out.println("\f");
I searched through a lot of forums and it seems that this simple question has gone unanswered for a long time.
So I took a good look at my ASCII list, and surely enough there is a mention of a code for form feed. Then I just tried using \f in Java and it is working wonders.
So if anyone still cares, try this:
CODE
System.out.println("\f");
This didn't work for me, it just printed the ♀ sign...
#26
Re: Clearing The Screen?
Posted 18 February 2009 - 07:30 AM
I guess ayman answered my question before I tried to compile it 
The question now here is... what could edvella have done to make this work? What OS are you using? If Java's escape string for "f" is the ♀ sign... why in the world and on what OS would this clear the console window?
The question now here is... what could edvella have done to make this work? What OS are you using? If Java's escape string for "f" is the ♀ sign... why in the world and on what OS would this clear the console window?
#27
Re: Clearing The Screen?
Posted 18 February 2009 - 07:46 AM
actually the \f escape sequence is simply a form feed or a page break..it doesnt clear the console
#28
Re: Clearing The Screen?
Posted 18 February 2009 - 07:48 AM
Depending on the choice of IDE, you can't clear the "console" screen.
#29 Guest_JavaS*
Re: Clearing The Screen?
Posted 04 March 2010 - 11:48 PM
The System.Console.Clear(); command doesnt work in java
#30
Re: Clearing The Screen?
Posted 04 March 2010 - 11:56 PM

New Topic/Question
This topic is locked



MultiQuote







|