0

This is a sample code. I want a progress bar / progress dots with message before printing output..

import java.util.Scanner;

public class Main {
  public static void main(String[] args) 
  {
    Scanner input = new Scanner (System.in);
    System.out.print("Input your first name: ");
    String fname = input.next();
    System.out.print("Input your last name: ");
    String lname = input.next();
    System.out.println();
    System.out.println("Hello \n"+fname+" "+lname);
  }
}

The output should be something like Sample Output:

Input your first name: James

Input your last name: Smith

---Please wait for a few second.. Loading. . . . your name - with blinking dots or stars(or what do they say?) Then Print the output

**Hello James Smith

Atahan Atay
  • 363
  • 2
  • 15

1 Answers1

1

To print a "loading" like progress bar on the console, you may do something like what is being done in this block. (I have limited this example upto 30 characters; you may want to bring in your own logic to control that.)

int count = 1;
try{
    while( true ){
        Thread.sleep( 500 );

        for( int i = 0; i < count; i++ ) System.out.print( "-" );
        System.out.print( "\r" );

        count++;
        if( count >= 30 ) break;
    }
}
catch( Exception e ){
    e.printStackTrace();
}

However, IDEs have settings on how to display the control characters. So, you may see that the dots are being printed on new lines instead of replacing the previous set. On Eclipse, you can control that with this setting, which comes on a right-click on the console.

enter image description here

Shortcoming: This doesn't solve the "coloured" dots problem. For this, there is some attempt at How to print color in console using System.out.println?. You may check that out.

Sree Kumar
  • 2,012
  • 12
  • 9