Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to type of the same line of printed text again without deleting the text that was previously outputted in Java

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

()

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading