Home > DeveloperSection > Forums > How to use of the Console class in java i/o
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    July-26-2016 7:54 AM

 Java Java I/O 
Ratings:


 1 Reply(s)
 207  View(s)
Rate this:

Hi buddy 

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

thanks



Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    July-26-2016 8:03 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick