5

So, basically what I want to do is have a percentage. So it goes up 0%, 1% and so on but, instead of having it all printed, I want the %1 percent replaced with 2%. So I know how to do the basic for loop but I don't know how to use \r or \b because in my compiler all I'm getting is it printing it all in one line.

package test;

public class est
{
    public static void main(String[] args) 
    {       
        for(int i = 0; i <= 100; i++ )
        {
            System.out.print("\rPercent = " + i + "%");
        }
    }
}

output:

Percent = 0%
Percent = 1%

...

Percent = 99%
Percent = 100%

So it prints 100 lines; but I only want that information on one line.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Isaiah Mitchell
  • 75
  • 3
  • 11

3 Answers3

4

I tried using '\r' in the sysout and it worked for me in Intellij Idea console & running it from terminal of mac osx.

 public static void main(String[] a) throws InterruptedException {
    int progress = 10;
    for(progress = 10; progress<=100; progress+=10) {
        System.out.print("\rPercent = " + (progress) + "%");
        Thread.sleep(3000);
    }
}

You may want to give it a try. However, I strongly believe it will be dependent upon the underlying console and OS.

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
  • this did not work on eclipse when I tried to run it, it printed individual lines. However, when I ran it in terminal, it worked great. Nice Job. –  Apr 25 '17 at 03:29
2

The simple answer is: System.out is not an appropriate basis when you strive to provide "enhanced user experience". Maybe in this case, as you can use \r to delete on the same line. But as soon as you need / want to do a println() that context is fixed; and you can't change it anymore.

And even then: "going back" and erasing information can't be done in a way that would work on each platform.

You have three options:

  • simply accept it. In the real world, command line output is often ugly and most people accept that.
  • look into "curses" based libraries, as those allow for "placing" and "resetting" your "cursor". But I actually can't say how platform independent those libraries are.
  • go down the full nine yards and consider creating a real Guide. But of course, that is the most expensive solution to your problem.
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • _System.out is not an appropriate basis when you strive to provide "enhanced user experience"_... is there an appropriate basis? –  Apr 25 '17 at 02:57
  • so one can not update console, or replace text in console? –  Apr 25 '17 at 02:59
  • sorry, that is because I was confused by what you were saying. I meant to Re-Upvote it, but got sidetracked with a new question. I just upvoted now. :D –  Apr 25 '17 at 23:13
  • Is there any libraries that have a console built into a jframe which I could you to go about creating a rogue like? – Isaiah Mitchell Apr 26 '17 at 20:09
2

For eclipse you need to configure the console to interpret the Ascii charaters like so

enter image description here

Java Main
  • 1,521
  • 14
  • 18