---
title: "How to use of the Console class in java i/o"  
description: "How to use of the Console class in java i/o"  
author: "Samuel Fernandes"  
published: 2016-07-26  
updated: 2016-07-26  
canonical: https://www.mindstick.com/forum/34156/how-to-use-of-the-console-class-in-java-i-o  
category: "java"  
tags: ["java i/o"]  
reading_time: 5 minutes  

---

# How to use of the Console class in java i/o

## Hi buddy

How to use of the [Console](https://www.mindstick.com/blog/210/the-console-class) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) in javai/o programming.Please provide me example with programme code.\
thanks

## Replies

### Reply by Mark Devid

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;

                          }

}
```


---

Original Source: https://www.mindstick.com/forum/34156/how-to-use-of-the-console-class-in-java-i-o

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
