How do I create a console progress bar?

The code below demonstrates how to create a progress bar in a console program. The trick is to print the progress status using a System.out.print() and add a carriage return character ("\r") at the end of the string to return the cursor position to the beginning of the line and print the next progress status.

package org.kodejava.example.lang;

public class ConsoleProgressBar {
    public static void main(String[] args) {
        char[] animationChars = new char[]{'|', '/', '-', '\\'};

        for (int i = 0; i <= 100; i++) {
            System.out.print("Processing: " + i + "% " + animationChars[i % 4] + "\r");

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Processing: Done!          ");
    }
}

How do I use Console class to read user input?

In the previous example, How do I read user input from console using Scanner class?, we use the java.util.Scanner class to read user input. In this example we use another new class introduced in the Java 1.6, the java.io.Console class.

The Console class provide methods like readLine() and readPassword(). The readPassword() method can be use to read user’s password. Using this method the user’s input will not be shown in the console. The password will be returned as an array of char.

package org.kodejava.example.io;

import java.io.Console;

public class ConsoleDemo {

    public static void main(String[] args) {
        // Get a console object, console can be null if not available.
        Console console = System.console();

        if (console == null) {
            System.out.println("Console is not available.");
            System.exit(1);
        }

        // Read username from the console
        String username = console.readLine("Username: ");

        // Read password, the password will not be echoed to the
        // console screen and returned as an array of characters.
        char[] password = console.readPassword("Password: ");

        if (username.equals("admin") && String.valueOf(password).equals("secret")) {
            console.printf("Welcome to Java Application %1$s.%n", username);
        } else {
            console.printf("Invalid username or password.%n");
        }
    }
}

The result of the code snippet above is:

Username: admin
Password:
Welcome to Java Application admin.

How do I read user input from console using java.util.Scanner class?

In JDK 1.5 a java.util.Scanner class was introduce to handle user input in console application. This class enable us to read string, integer, long, etc in the console application.

package org.kodejava.example.util;

import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read string input for username
        System.out.print("Username: ");
        String username = scanner.nextLine();

        // Read string input for password
        System.out.print("Password: ");
        String password = scanner.nextLine();

        // Read an integer input for another challenge
        System.out.print("What is 2 + 2: ");
        int result = scanner.nextInt();

        if (username.equals("admin")
                && password.equals("secret") && result == 4) {
            System.out.println("Welcome to Java Application");
        } else {
            System.out.println("Invalid username or password, " +
                    "access denied!");
        }
    }
}

The result of the code snippet:

Username: admin
Password: secret
What is 2 + 2: 4
Welcome to Java Application