0

I've tried using \033[H\033[2J. and System.out.flush(); but so far, no luck

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException {
    Scanner myObj = new Scanner(System.in);
    
    System.out.println("Hello User,");
    System.out.println("Please Enter Username");
    String UserName = myObj.nextLine();
    System.out.println("Welcome, " + UserName);
    System.out.println("Please Enter Your Password");
    String Password = myObj.nextLine();
    if ("Vaska".equals(Password)){
        //Clear Console
        System.out.println("You Have Now Gained Access To The Program, Have Fun!");
    }else{
        //Clear Console
        System.out.println("Sorry " + UserName + ", You Have Entered The Wrong Password, Please Restart The Application");
        System.exit(0);
    }
    
}

I've also tried using the System.getProperty() Method, but it always comes back as an error.

Blue_A55T
  • 21
  • 4

1 Answers1

0

If

System.out.print("\033[H\033[2J");
System.out.flush();

doesn't work maybe means that your terminal doesn't support ANSI escape code.

Have you tried this instead?

public class ClearScreen{
public  static void main (String [] args){
    System.out.println("Hello World");
    ClearConsole();
}

public static void ClearConsole(){
    try{
        String operatingSystem = System.getProperty("os.name") //Check the current operating system
          
        if(operatingSystem.contains("Windows")){        
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls");
            Process startProcess = pb.inheritIO.start();
            startProcess.waitFor();
        } else {
            ProcessBuilder pb = new ProcessBuilder("clear");
            Process startProcess = pb.inheritIO.start();

            startProcess.waitFor();
        } 
    }catch(Exception e){
        System.out.println(e);
    }
}
}