0

I have to update my console.

Here is an example code:

public class Tester {
    public static void main(String[] args) {
        System.out.print("abcd\n");
        System.out.print("efgh");
        System.out.print("\r\r\rijkl");
}
}

My output:

abcd
ijkl

Expected output :

ijkl
efgh

I'm also tried this:

        System.out.print("abcd\n");
        System.out.print("efgh\n");
        System.out.print("\033[0;0H");//\033[0;0H is an escape sequence to positioning the console cursor(Referred From Internet).
        System.out.print("ijkl");

For the above Code the output is :

abcd
efgh
←[0;0Hijkl

While I try online compiler the above code working fine. see here

But it is not working in windows and linux OS

Please Tell me What change i have to make to get my expected output.

Thank you all.

Dhanasekaran Don
  • 294
  • 1
  • 14
  • What...? I used your codes and I got the expected output. Well, both your current output and your expected output are weird... – Jai May 21 '18 at 07:35
  • You are using a terminal that does not translate '\r' (A carriage return) into a line feed. Therefore, the cursor returns to the beginning of the line (three times), and overwrites what was already there. – jr593 May 21 '18 at 07:49
  • Oh. I m using in Intellij IDE and also in normal comand Prompt both show same output @jr593 – Dhanasekaran Don May 21 '18 at 07:52

2 Answers2

1

You need to learn difference between the char literal \n and \r. It varies from system to system. While both of them are used in similar sense, yet they work differently. \r works as a Carriage Return. It simply push the cursor back to the start of the line. In your case it is exactly mimicking the same scenario.

When compiler compiles this piece of code System.out.print("abcd\n"); // prints abcd on console. System.out.print("efgh"); // prints efgh on console. System.out.print("\r\r\rijkl") // takes the cursor at the start of the line, overrides previous characters and print ijkl on console respectively. So, if you are using \r then try using `System.out.println' as it will print the literals in the new line, Though it will still move the cursor to the start of the line, first. But it will implicitly print characters on the second line.

For more information, refer to this question

Anus Kaleem
  • 544
  • 4
  • 16
  • could you please explain me what changes have to made to get the output? – Dhanasekaran Don May 21 '18 at 09:04
  • As Anus Kaleem said, the result is system dependent. Usually, `\r` means "Return at the beginning of the line". So, it doesn't allow you to move "up" in the output. Maybe you can use other guru tricks but again, that would be totally deperdent on your OS. – Bruno L May 22 '18 at 04:49
1

To see the difference between \r and \n you must run your code from a command prompt. Eclipse's Console (and similar for other IDEs) does not simulate the behavior of a full terminal and will move to the next line for both \r and \n. On the command line, however, \r will only move the cursor back to the beginning of the current line.

Adya
  • 1,084
  • 9
  • 17