Java Finding a Method for Incremental Output in Java

AI Thread Summary
In a discussion about creating a terminal-based Java program that displays incremental updates, participants explored methods to replace the previous output rather than appending new lines. It was noted that standard output alone cannot achieve this effect; instead, controlling the terminal window is necessary. Suggestions included using Java implementations of "curses" for terminal control and sending terminal control characters like backspace or carriage return ('\r') to overwrite the previous output. Additionally, an alternative approach was proposed, involving a graphical representation of progress using a line of characters that fills as progress increases. The conversation highlighted the importance of terminal capabilities in achieving the desired output format.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top