HOW TO USE OF THE CONSOLE CLASS IN JAVA I/O

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 476  View(s)
Ratings:
Rate this:

Hi buddy 

How to use of the Console class in javai/o programming.Please provide me example with programme code.

thanks

  1. Mark Devid

    Post:102

    Points:714
    Re: How to use of the Console class in javai/o

    Hi Guys 

    Try this  programming code

    public class ConsoleApp {

                              private static final int MAX_LOGINS = 3;

     

                              public static void main(String[] args) {

                                        ConsoleApp app = new ConsoleApp();

                                        if (app.login()) {

                                                    System.out.println("Thanks for logging in!");

                                        } else {

                                                    System.out.println("Login failed!");

                                        }

                              }

     

                              private boolean login() {

                                        Console console = System.console();

                                        boolean isAuthenticated = false;

                                        if (console != null) {

                                                    int count = 0;

                                                    do {

                                                              char[] pwd = console.readPassword("[%s]", "Password:");

                                                               isAuthenticated = authenticate(pwd);

                                                               

                                                                      // delete password from memory

                                                                Arrays.fill(pwd, ' ');

                                                                console.writer().write("\n");

                                                    } while (!isAuthenticated && ++count < MAX_LOGINS);

                                        }

                                        return isAuthenticated;

                              }

     

                              private boolean authenticate(char[] passwd) {

                                        char[] secret = { 'M', ‘i’, ‘N’, ‘D’, ‘S’, ‘T’, ‘I’, ‘C’, ‘K’};

                                        if (java.util.Arrays.equals(passwd, secret)) {

                                                    java.util.Arrays.fill(passwd, ' ');

                                                    System.out.println("Authenticated\n");

                                                    return true;

                                        } else {

                                                    System.out.println("Authentication failed\n");

                                        }

                                        return false;

                              }

    }

      Modified On Jul-26-2016 08:04:10 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!