3

I have a java application which run on cmd. I want to show progress while running this process. In this application 50 branches processing and I want to show after processed one branch 2% completed and after another branch processed 4% completed and so on. Problem is when I used

System.out.print("PROGRESS :" + branch_Vec.get(value).toString()+" : "+ df2.format(((a / total) * 100)) +" % \r");

as a out put it goes to new line each time it calls.

I want to show progress in same line.For example:

progress : AB : 2%
Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
Dilshan
  • 45
  • 2
  • 9

2 Answers2

1

The code System.out.print("\rComplete: "+percentage); should just work.

The \r is a carriage return, and will clear the current line. As long as you don't write a newline \n you can update the text.

Note that this does not work in the console in Eclipse, if you're using that to test.

Matthijs Bierman
  • 1,720
  • 9
  • 14
0

There is no way of doing it, at least in a simple way. The point is that the default System.out OutputStream either allows you to call print(), which prints characters one after another, or println(), which does the same as print(), but also puts a new line in the end of your text.

One non-elegant solution would be to clear the screen between the updates, so as to make it seem like it is printing in the same position. If you're using windows, for example, this would be:

Runtime.getRuntime().exec("cls");

Unfortunately, this line is system-dependant, as it calls the cls command.

Filipe Fedalto
  • 2,540
  • 1
  • 18
  • 21
  • Check out this [answer](http://stackoverflow.com/a/4207708/451518) to another question. It's possible and OP is talking about unexpected behaviour – default locale Mar 13 '13 at 10:13
  • That's interesting! It would have been a problem in old Mac OS (up to version 9) environments, but it will probably work in all platforms nowadays. – Filipe Fedalto Mar 13 '13 at 10:16