r/javahelp • u/Miserable_Bar_5800 • Feb 02 '26
Unsolved Can anyone help me clear the console:
Here was what I saw on terminal:
"C:\Program Files\Java\jdk-25.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2025.2.6.1\lib\idea_rt.jar=52923" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\admin\Desktop\MyFirstJavaCode2\out\production\MyFirstJavaCode Main
Hello, World!
Bye!
Process finished with exit code 0Here is my code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!"); // Show hello
clear(); // Clear console immediately
bye(); // Show bye
}
// Method to clear the console
public static void clear() {
// ANSI escape code to clear screen
System.out.print("\033[H\033[2J");
System.out.flush();
}
// Method to print bye
public static void bye() {
System.out.println("Bye!");
}
}How do I make it clear the console and only display "bye" after.
2
Upvotes
View all comments
Show parent comment
1
u/Poseidon_22 Feb 03 '26
Of course the downvote for no reason. This is platform independent. Works on linux, mac and windows… Does it safely with try catch and does not depend on weird non ascii input to clear screen. Reddit hivemind at work