How to delete previous character printed to console/terminal?
You might want to take a look at
System.out.printf
Look at the example shown here: http://masterex.github.com/archive/2011/10/23/java-cli-progress-bar.html
edit:
printf displays formatted strings, which means you can adapt that format and change it for your needs.
for example you could do something like:
String[] planets = {"Mars", "Earth", "Jupiter"};
String format = "\r%s says Hello";
for(String planet : planets) {
System.out.printf(format, planet);
try {
Thread.sleep(1000);
}catch(Exception e) {
//... oh dear
}
}
Using the formatted string syntax found here: http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax
As the comment says this solution is only limited to a singular line however dependent on your needs this might be enough.
If you require a solution for the whole screen then a possible solution would be (although quite dirty) would be to hook the operating system using JNA and get a handle on the console window, find its height and then loop println() to "clear" the window then redraw your output.
If you would like to read more then I can answer more questions or here is a link: https://github.com/twall/jna
How I can delete the System.out.print and to have a unique return
You can achieve your goal by carrying out the following steps:
1. Create an empty StringBuilder
object at the beginning of the cipher method (this object will hold the sequence of characters that forms the final String
to be returned by that method).
2. Replace each code line of the form (where x represents an int
variable)
System.out.print(Character.toString((char) x));
with the following (where (a) s represents the StringBuilder
object mentioned in part 1 above, and (b) this resulting line appends the char
representation of x to s):
s.append((char) x);
3. Once your StringBuilder
sequence is complete, obtain the String
representation of that sequence with the following method (where s is the StringBuilder
object in question):
s.toString();
Here's a modified version of the cipher method which utilizes the above approach:
public static String cipher(String input2, Integer number) {
StringBuilder builder = new StringBuilder(input2.length());
int k = number;
int lowerBoundlc = 97;
int upperBoundlc = 122;
int lowerBounduc = 65;
int upperBounduc = 90;
for (int i = 0; i < input2.length(); i++) {
char c = input2.charAt(i);
int j = (int) c;
if ((lowerBoundlc <= j && j <= upperBoundlc) || (j == 45)) {
if (j == 45) {
builder.append((char) j);
} else {
int addk = j + k;
if (addk > upperBoundlc) {
int lowercase = addk % upperBoundlc;
int resultlc = lowerBoundlc + lowercase -1;
builder.append((char) resultlc);
} else {
builder.append((char) addk);
}
}
} else {
if ((lowerBounduc <= j && j <= upperBounduc) || (j == 45)) {
int addnewk = j + k;
if (addnewk > upperBounduc) {
int uppercase = addnewk % upperBounduc;
int resultuc = lowerBounduc + uppercase -1;
builder.append((char) resultuc);
} else {
builder.append((char) addnewk);
}
}
}
}
return builder.toString();
}
Clear the console in Java
You can try something around these lines with System OS dependency :
final String operatingSystem = System.getProperty("os.name");
if (operatingSystem .contains("Windows")) {
Runtime.getRuntime().exec("cls");
}
else {
Runtime.getRuntime().exec("clear");
}
Or other way would actually be a bad way but actually to send backspaces to console till it clears out. Something like :
for(int clear = 0; clear < 1000; clear++) {
System.out.println("\b") ;
}
Adding and removing text from the console in Java
You can delete text by using the backspace character \b
. For example:
System.out.print("\b");
An example:
public class Test {
public static final String[] frames = {"|", "/", "-", "\\"};
public static void main(String[] args) throws Exception {
System.out.println();
for (int ctr = 0; ctr < 50; ctr++) {
System.out.print(frames[ctr % frames.length]);
Thread.sleep(250);
System.out.print("\b");
}
}
}
Note, however, that this may not work in the Eclipse console, but will work outside of Eclipse.
What is a regex to replace all System.out.println() with empty space?
you can use following regex and simply do find and replace. There is a option for regex find and replace in Eclipse
.
System.out.println\(([^;].*\)*);
I tested for following
System.out.println(" ");
System.out.println(" fdfdf");
System.out.println(") fdfdf");
System.out.println(";");
System.out.println();
This may help you to find regex find and replace in Eclipse
Here is regex online test. I put some data there. any one can test with any thing.
How can I replace text that's been printed out on screen already in Java?
That has nothing to do with java actually. Rather you want the console you are displaying in to overwrite the text.
This can be done (in any language) by sending a carriage return (\r in Java) instead of a linefeed (\n in Java). So you never use println(), only print() if you want to do that. If you add a print("\r") before the very first print it should do what you want.
BTW it would be far more elegant and simple if you built the entire time in a string and then output it all at once :)
Related Topics
How to Add Image in a Textview Text
Can the Android Sdk Work with Jdk 1.7
Create a New Textview Programmatically Then Display It Below Another Textview
Volley - Sending a Post Request Using JSONarrayrequest
JSONparser from Androidhive Tutorial, Nosuchmethoderror in Defaulthttpclient
Date Object Simpledateformat Not Parsing Timestamp String Correctly in Java (Android) Environment
How to Resolve Host "<Url Here>" No Address Associated with Host Name
How to Check If Current Thread Is Not Main Thread
Why Does "String".Getbytes() Work Different According to the Operation System
Failed to Run Sdkmanager --List with Java 9
How to Find the Currently Running Applications Programmatically in Android
How to Create a Bks (Bouncycastle) Format Java Keystore That Contains a Client Certificate Chain
Java Trayicon Using Image with Transparent Background
Android: How to Change the Color of the Datepicker Divider
Android M Permissions: Onrequestpermissionsresult() Not Being Called