To clear the console after taking input in Java, you can use the following code:
refer to:lautturi.comimport java.io.IOException; import java.util.Scanner; public class ClearConsole { public static void main(String[] args) throws IOException, InterruptedException { Scanner scanner = new Scanner(System.in); System.out.print("Enter something: "); String input = scanner.nextLine(); // process the input // clear the console new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); } }
This code uses the Scanner
class to read input from the console, and then uses the ProcessBuilder
class to execute the "cmd" command with the "/c" option and the "cls" command, which clears the console. The inheritIO
method is used to redirect the output of the command to the current terminal, and the waitFor
method is used to wait for the command to complete before continuing with the program.
Note that this code will only work on Windows systems. On other systems, you can use a similar approach by calling the appropriate command for clearing the console. For example, on a Unix-based system, you can use the "clear" command.