11

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?

F.P
  • 16,592
  • 33
  • 120
  • 188

3 Answers3

7

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

Chandra Sekhar
  • 17,770
  • 14
  • 76
  • 115
  • I'm running on Windows XP currently - maybe that makes a difference? – F.P Mar 05 '12 at 12:55
  • Through eclipse... On OS X, I use IntelliJ where it works just fine. I'll give it a try on command prompt now. – F.P Mar 05 '12 at 13:19
  • As I have already mentioned, problem is with Eclipse not with Java. Without using extra tool like Intellij, it will not work in eclipse. – Chandra Sekhar Mar 05 '12 at 13:23
1

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".

fishinear
  • 5,871
  • 3
  • 33
  • 81
0

By "Windows" you mean cmd.exe? And you want to overwrite to the line you previously output, right? Unfortunately I think you need to invoke this Win32 API somehow in order to achieve it

Maybe other Java gurus can provide better answer ...

Community
  • 1
  • 1
nodakai
  • 7,363
  • 3
  • 27
  • 57
  • I am running the program in eclipse on a windows machine. Haven't tried it on CMD yet. – F.P Mar 05 '12 at 12:52