Home > Blockchain >  Is there a way to type of the same line of printed text again without deleting the text that was pre
Is there a way to type of the same line of printed text again without deleting the text that was pre

Time:02-26

I have been trying to figure out how to edit an already printed out line of text without deleted the text that you just wrote.

I have thought that a work around of this problem is to just “edit” it by doing \r and reprinting the coding every single time.

For example if I print out

()

But then I wanted to add “hi” to it, it would then insert it to become

(“hi”)

If anyone knows the answer it would be very nice if you could respond to help me out, thanks so much.

CodePudding user response:

This depends on your terminal.

If your terminal handles ANSI escape sequences, the program below will print EDCBA. If it doesn't it will print:

AAAAA
BBBB
CCC
DD
E

Xterm works.

public class Test {
    public static void main(String[] args) {
        System.out.println("AAAAA");
        System.out.print("\033[1A");
        System.out.println("BBBB");
        System.out.print("\033[1A");
        System.out.println("CCC");
        System.out.print("\033[1A");
        System.out.println("DD");
        System.out.print("\033[1A");
        System.out.println("E");
    }
}

This doesn't insert new text in the previous line, so you need to do those operations in an in-memory buffer and refresh an entire line.

Have a look at this question for some more libraries which may support a wider range of terminals.

CodePudding user response:

I may be misunderstanding your question, but you could use System.out.print() multiple times to add something onto the end of the line.

  • Related