1

I have a program in c++, that loops over a lot of calculations. For each loop I use printf to print out how far the calculations are in percent, like

"Calculations are 64.7 % done."

Each print out overwrites the previous, thus using only one line in the console instead of printing several lines under each other.

Is that possible in java? The printf in java prints out a new line every time.

Or do you guys have some other idea as to how I can easily see the progress.?

Troels
  • 201
  • 2
  • 8

2 Answers2

7

Yes, it is possible. User System.out.print("your text\r"). It prints line and does not move cursor to the next line. The sequence \r makes cursor to move to the beginning of the current line, so the next line will be printed again from the same position.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    Tried it and confirm that it works. Just not in Eclipse's console. – Marko Topolnik Jul 29 '12 at 11:40
  • That depends on how the current console interprets the '\r' character and thus can vary a lot. – Martin York Jul 29 '12 at 14:24
  • @LokiAstari The standard requires `'\r'` to map to carriage return, which _should_ behave as specified in ASCII (and described by AlexR). In practice, of course, you're right---this all depends on how the output device decides to interpret it: for a console window or a printer, I would say that not doing a carriage return would be a very poor implementation---amost on the same level as displaying a glyph which looks like a b when you output an `'a'`. But for other types of devices, who knows. – James Kanze Jul 29 '12 at 15:43
  • @JamesKanze: Control characters are not the same as glyphs. And their interpretation varies greatly (even in the comments around this question it does not work universally). This is why there is a whole library dedicated to control cursor movements (see ncurses) agnostically across all consoles. – Martin York Jul 29 '12 at 19:09
  • @LokiAstari No, but the basic principle is the same. The code point determines an action which depends on the receiving device. In some ways, in fact, the control characters (at least some of them) are more strictly defined than the glyphs---the appearance of an `'a'` can vary enormously from one device to another, whereas carriage return means what it says: in left to right contexts, position the next write position at the start of the line. Any device which does _not_ do this is broken. – James Kanze Jul 30 '12 at 08:03
  • @LokiAstari Of course, there are very few such well defined actions, and curses et al. are necessary to handle more general placement. – James Kanze Jul 30 '12 at 08:04
  • @JamesKanze: My point is that it is **not well defined** and trying to divert the conversation to glyphs is meaningless and not relevant. As you can already see by it not working for the OP (in eclipse). – Martin York Jul 30 '12 at 08:08
1

From How can I return to the start of a line in a console? I understand that

System.out.print('\r');

will return to the start of the line. That should work for printf as well.

Community
  • 1
  • 1
steffen
  • 8,572
  • 11
  • 52
  • 90