Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java question

  1. Feb 21, 2006 #1
    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.
  2. jcsd
  3. Feb 21, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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:


    - Warren
  4. Feb 21, 2006 #3


    User Avatar
    Science Advisor
    Homework Helper

    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.)
  5. Feb 21, 2006 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You could display the information in an alternate manner. For example, you could plot a graph:


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


    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)
  6. Mar 4, 2006 #5
    Thanks for all of your suggestions guys, but I did find out a good way to do it:



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

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Java question
  1. Java Question (Replies: 2)

  2. Java Question (Replies: 2)

  3. Java Question (Replies: 3)

  4. Question about Java (Replies: 4)