Finding a Method for Incremental Output in Java

  • Context: Java 
  • Thread starter Thread starter Yoss
  • Start date Start date
  • Tags Tags
    Java Method Output
Click For Summary

Discussion Overview

The discussion centers on methods for achieving incremental output in a Java terminal application, specifically how to update the output to show progress percentage without creating new lines for each update. The scope includes programming techniques and terminal control in Java.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant inquires about methods to replace terminal output in Java to show incremental updates of percentage completion.
  • Another participant states that standard output cannot achieve this and suggests using terminal control libraries like "curses" in Java.
  • A different participant proposes using terminal control characters or backspace to move the cursor for updating the output.
  • Another suggestion involves displaying progress graphically on the terminal by filling a line with characters as progress is made.
  • A participant mentions finding a solution using the backspace character ('\b') to achieve the desired output effect.
  • One participant requests further details on the solution found, indicating interest in applying it to a different context (printing a running throughput average).

Areas of Agreement / Disagreement

Participants express various methods and ideas, but there is no consensus on a single solution. Multiple competing views on how to achieve the incremental output remain unresolved.

Contextual Notes

Some methods discussed depend on specific terminal capabilities and may not be universally applicable across different environments. The effectiveness of certain approaches may vary based on the terminal type and settings.

Yoss
Messages
27
Reaction score
0
I was curious if anyone knows if there are any methods to do the following in java.

I'm writing a terminal based program in java, and it will be doing incremental updates. I want to output to the terminal the percentage done, but to replace the last output. For example:

$>java jprog
Percent completed...0%
...
$>java jprog
Percent completed...5%.

Instead of
$>java jprog
Percent completed...0%
Percent completed...5%
...

I checked the java API's a little, in the PrintStream and OutputStream classes, but couldn't find any methods like that. Anybody know? Thanks.
 
Technology news on Phys.org
Standard output by itself cannot do such a thing. To accomplish this, your Java program actually needs to control the terminal window which shows the output. I suggest you look into implementations of "curses" in Java, for example:

http://sourceforge.net/projects/javacurses/

- Warren
 
I'm not sure if your Java app can read termcap/terminfo, but it should be possible to send terminal control characters (or maybe backspace) to move the cursor around. (That's what curses does after all.)
 
You could display the information in an alternate manner. For example, you could plot a graph:


#===1===2===3===4===5===6===7===8===9===#

And fill up the line below it one character at a time as it progresses. For example, at 22.5%, it would look like:

#===1===2===3===4===5===6===7===8===9===#
#===1===2=

and the cursor is at the end of the second line, ready to display another character as necessary.

You can make this prettier if you're willing to assume an equal width font.



If you're willing to assume a *NIX-style environment, the character '\r' is a carriage return (without the line feed) -- it positions the cursor at the beginning of the current line. You could use this to achieve the effect you describe. (Backspace would usually work too, as mentioned)
 
Thanks for all of your suggestions guys, but I did find out a good way to do it:

'\b'.

:E

edit: oops Nate didn't see your reply!
 
Yoss,
Care to share what you did? I am writing a load tool and I want to print a running throughput average to the console.

Thanks.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K