Move the print position of the command-line interface to Java without using the external library

advertisements

In C, I recalled that I can move the invisible caret around the command line interface screen with respect to the line and character position, meaning I can make the program print any text anywhere on the screen. Do we have such command in Java?

For instance, here is a pseudocode in C:

int main(){
    printf("launching program\n");
    moveTo(4,3); //move to line 4 at character index 3 on the screen.
    printf("AAA");
    moveTo(3,0); //move to line 3 at character index 0 on the screen.
    printf("BBB");
    moveTo(2,1); //move to line 2 at character index 1 on the screen.
    printf("CCC");
    return 0;
}

This will give the following output in the command line interface:

launching program

 CCC
BBB
    AAA

Do we have an equivalent method in Java without using any external or third party library in this case?


The ability to do this is a property of the terminal, not the language. So in principal, if you're connected to a sufficiently capable terminal emulator, then yes, of course it's possible.

The purpose of a library like ncurses is to abstract away the gory details of terminal-dependent cursor movement, etc. You don't need something like ncurses, you could always just directly emit the appropriate codes for your target terminal.

By, "is there an equivalent method in Java," do you mean are there libraries which also can provide you terminal-agnostic abstractions? Yes (see the other responses). But nothing is going to make every host system to the JVM provide a VT100 emulator. For example, good luck on Windows. In this sense, 2D graphics in Java are more universal than the terminal environment!