Home > OS >  Java: Delete newline which is already printed
Java: Delete newline which is already printed

Time:11-24

Given the Java Code:

System.out.println("hello"); 
System.out.println();
//insert here
System.out.prinln(" world");

Is there a way to delete the new line where we insert something into "//insert here" without modifying the rest of the Code such that the Output will go from:

Hello

World

into:

Hello World

I already search for solutions for example print('\b') or Thread.sleep(1000); but the first one does exactly the opposite of what I want and the latter just crushes everything.

Edit: I know that one could simply use print istead

CodePudding user response:

There is no really portable way to do it, because not every terminal type supports cursor movement up. However, you can try ASCII control sequences. For example ESC [2A moves cursor 2 lines up. Unfortunately, positioning cursor after the printed 'Hello' is impossible without knowing the length of 'Hello' - the information about how many characters are already printed on this line is not available.

CodePudding user response:

you can use System.out.print instead println

  • Related