Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a carriage return in java on windows on console

On my OS X machine, the following line gives me a nice and easy way to track the state of my loops:

for (int index = 0; index < 100; index++)
    for (int subIndex = index; subIndex < 100; subIndex++)
        System.out.print("\r" + index + "/" + subIndex + "       ");

But when I try to run the same thing on windows, it prints out newlines instead of a carriage return. How can I achieve the same simple method of tracking the process on windows?

like image 817
F.P Avatar asked Mar 05 '12 12:03

F.P


2 Answers

I had the statement and it worked in the command prompt

System.out.println("This is Java"+'\r'+"That");

and gives me output as

That is Java

That means it works perfectly.

Note: I run it in Windows 7 with JDK 7 and simple notepad.

It is the problem of eclipse, it will take \r as a new line character and will print

This is Java
That

as output

like image 35
Chandra Sekhar Avatar answered Sep 28 '22 00:09

Chandra Sekhar


If you are on Eclipse, you have to enable the control character option on the Console Window.

To enable it, open the Eclipse preferences and select Run/Debug > Console. Then select "Interpret ASCII control characters" and "Interpret Carriage Return (\r) as control character".

like image 94
João Marcelo Torres Avatar answered Sep 27 '22 23:09

João Marcelo Torres