简体   繁体   中英

Progress colored with Dots/Stars in Java

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

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.

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM