Java question


by Yoss
Tags: java
Yoss
Yoss is offline
#1
Feb21-06, 12:20 PM
P: 27
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.
Phys.Org News Partner Science news on Phys.org
NASA's space station Robonaut finally getting legs
Free the seed: OSSI nurtures growing plants without patent barriers
Going nuts? Turkey looks to pistachios to heat new eco-city
chroot
chroot is offline
#2
Feb21-06, 01:01 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,424
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
NateTG
NateTG is offline
#3
Feb21-06, 01:09 PM
Sci Advisor
HW Helper
P: 2,538
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.)

Hurkyl
Hurkyl is offline
#4
Feb21-06, 03:42 PM
Emeritus
Sci Advisor
PF Gold
Hurkyl's Avatar
P: 16,101

Java question


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)
Yoss
Yoss is offline
#5
Mar4-06, 09:47 PM
P: 27
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!
johny_doe
johny_doe is offline
#6
Mar15-06, 02:47 PM
P: 20
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.


Register to reply

Related Discussions
JLabel java question. Engineering, Comp Sci, & Technology Homework 1
a question about binary tree(java) Programming & Computer Science 2
question about this java program Programming & Computer Science 6
Java Question Programming & Computer Science 2
Java Question Programming & Computer Science 2