Solved

percentage counter in a command window

Posted on 2003-10-27
10
175 Views
Last Modified: 2008-02-20
I am trying to implement a counter on a command line that counts how much, % wise a file has been tranfered from a client to a server.

I was just wondering how I would be able to update the % on the same line in a System.out.println(""); command, so that I wouldnt have to have multiple lines in the terminal.

An example is like when you format your Hard Drive in DOS and it says 1% complete etc all the way up to 100%, thats what I am trying to acheive.


Thanks for any help.

0
Comment
Question by:welsh_boy
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9629746
Can't be done on the command line without using special JNI-based APIs
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9629870
I just had an idea and tried this. Perhaps you can do something with it?

for(int i = 0;i < 10;i++) {
          System.out.print(i);
          Thread.sleep(1000);
          System.out.print('\010');
}
0
 
LVL 2

Expert Comment

by:smitty22
ID: 9629881
What you could do instead is something like this:

0%                                                                               100%
|||||||||||||||||||||||||

Where you print the first line, calculate your scale based on file size (i.e. how much of the file does one "|" count for, then just use System.out.print("|") to print out another bar on the same line when the next unit has been transferred.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:welsh_boy
ID: 9629979
good idea smitty.

I have a giveFileLength() method so I can get the size of a file (as long). And each packet that is tranfered is Max 128 bytes, with 1 byte being a packet code, so effectivly I have a max of 127bytes of the file coming over at one time.


pseudo code so far.

System.out.print("0%                                               100%")
while (packetsrecived < giveFileLength())
{
System.out.print("|")
}

Not sure how to further implement it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9629999
And did you get the code i posted working? ;-)
0
 
LVL 2

Assisted Solution

by:smitty22
smitty22 earned 100 total points
ID: 9630161
You will probably want to set the number of bars to use ahead of time.  The code you have so far will probably print a large number of bars by the time the file downloads.

I would choose something between 40-60 characters long, then before each file is downloaded calculate how many bytes one bar stands for.

So if you use 40 bars = 100%, then for an 80 byte file each bar would represent 2 bytes.  So, after downloading 2 bytes, you would _then_ print out the next bar.

There are a number of ways you could implement this, but you could keep track of how many bars you've printed so far, then use packetsreceived to calculate how many more you need to print out to accurately represent the current download state.


**NOT TESTED**
> assuming packetsreceived is the number of bytes received so far

System.out.print("0%                                  100%");  // total width of 40 characters
int scale = fileSize / 40;
int count = 0;
int calc;
while ( packetsreceived < fileSize ) {
   calc = packetsreceived / scale;
   if ( calc > count  ) {
      for ( ; count <= calc; count++ ) {
         System.out.print("|");
      }
   }
}
0
 
LVL 2

Expert Comment

by:smitty22
ID: 9630165
I guess you could get rid of the if(calc > count) b/c it's taken care of in the for loop.
0
 

Author Comment

by:welsh_boy
ID: 9630172
yeh CEHJ

but wont that restrict me to having to have the estimated time that it will take to transfer the file?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9630239
No - it's nothing to do with time - the sleep is just to simulate a lengthy process so you can see the numbers change
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 9630383
Here's a full demo:

public class TestPercent {

      public static void main(String[] args) throws Exception {
            PercentageDisplayer pd = new PercentageDisplayer(500);
            for(int i = 1;i <= 5;i++) {
                  pd.update(100);
                  Thread.sleep(500);
            }
      }

      static class PercentageDisplayer {
            long extent;
            long received;

            public PercentageDisplayer(long extent) {
                  this.extent = extent;
            }

            public void update(long increment) {
                  for(int i = 0;i < 12;i++) {
                        System.out.print('\010');
                  }
                  received += increment;
                  long percentage = 100 * received / extent;
                  System.out.print("" + percentage + "% received");
            }
      }
}
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 88
servlet example 11 42
expectj telnet failing 5 38
hibernate example using maven 12 44
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question