I want my program to be able to erase all the text it’s printed. Every time it prints something, I call my print() method instead of System.out.print(), but then when I call the erase() method it doesn’t seem to do anything in the Windows 7 console. I can’t figure out what’s up; I ran a test separately that confirmed that \b does erase characters but for whatever reason it won’t work in erase(). Are the backspace characters erasing each other or something?
EDIT: I’ve run some more tests. It looks like \b won’t overwrite newline characters. So I guess I need a way to do that.
public static int textLength = 0;
public static void erase() {
for (int i = 0; i < textLength; i++) {
System.out.print('\b');
}
textLength = 0;
}
public static void print(String s) {
textLength += s.length();
System.out.print(s);
}
EDIT:
Seems like this is not the solution you are looking for. When using
you need to be aware that this does not erase what was already printed. Instead each backspace moves your cursor back one character. To then actually erase what is there you need to overwrite it with some other text.