Search code examples
javaconsole-application

Move console cursor to specified position


I'm writing a simple console application (80x24) in Java.

Is there a gotoxy(x,y) equivalent for the console?


Solution

  • If by gotoxy(x,y), you want to reposition your cursor somewhere specific on the console, you can usually use VT100 control codes to do this. See http://www.termsys.demon.co.uk/vtansi.htm.

    Do something like

    char escCode = 0x1B;
    int row = 10; int column = 10;
    System.out.print(String.format("%c[%d;%df",escCode,row,column));
    

    Which should move the cursor to position 10,10 on the console.