Example usage for java.lang System console

List of usage examples for java.lang System console

Introduction

In this page you can find the example usage for java.lang System console.

Prototype

public static Console console() 

Source Link

Document

Returns the unique java.io.Console Console object associated with the current Java virtual machine, if any.

Usage

From source file:Main.java

public static void main(String[] args) {
    Console console = System.console();
    String username = console.readLine("Username: ");
    char[] password = console.readPassword("Password: ");

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

        Arrays.fill(password, ' ');
    } else {// w  ww .j a v a  2s  .c  o  m
        console.printf("Invalid username or password.\n");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Console cnsl = System.console();

    if (cnsl != null) {
        System.out.print("Enter name : ");
        Scanner scan = new Scanner(cnsl.reader());
        while (scan.hasNext()) {
            String str = scan.next();
            System.out.println(str);
        }/*from   www . jav  a 2s  . com*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    Console console = System.console();

    if (console == null) {
        System.out.println("Console is not available");
        System.exit(1);//  ww w  .  j  av a 2s. c o  m
    }

    char[] password = "java2s.com".toCharArray();

    char[] passwordEntered = console.readPassword("Enter password: ");

    if (Arrays.equals(password, passwordEntered)) {
        System.out.println("\n Access granted \n");
        Arrays.fill(password, ' ');
        Arrays.fill(passwordEntered, ' ');
        System.out.println("OK ...");
    } else {
        System.out.println("Access denied");
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Console cons = System.console();
    if (cons == null) {
        System.out.println("Console is null");
        System.exit(-1);/*w  w  w  . j ava2  s  . co  m*/
    } else {
        System.out.println("Enter text, or \"z\" to exit:");
        while (true) {
            char c = (char) cons.reader().read();
            System.out.println("" + c);
            if (c == 'z') {
                System.exit(0);
            }
        }
    }
}

From source file:PasswordPromptingDemo.java

public static void main(String[] args) {
    Console console = System.console();

    if (console == null) {
        System.out.println("Console is not available");
        System.exit(1);//from ww w  .j  a v a 2s  .co  m
    }

    char[] password = "mustang".toCharArray();

    char[] passwordEntered = console.readPassword("Enter password: ");

    if (Arrays.equals(password, passwordEntered)) {
        System.out.println("\n Access granted \n");
        Arrays.fill(password, ' ');
        Arrays.fill(passwordEntered, ' ');
        System.out.println("OK ...");
    } else {
        System.out.println("Access denied");
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String fmt = "%2$5s %3$10s%n";

    Console cnsl = System.console();

    if (cnsl != null) {
        String alpha = cnsl.readLine(fmt, "Your", "Name: ");

        System.out.println("Name is: " + alpha);

        char[] pwd = cnsl.readPassword(fmt, "Enter", "Password: ");

        System.out.println("Password is: " + pwd);
    }/*from w ww  .ja  v a  2  s. c o m*/

}

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
        System.err.println("sales: unable to obtain console");
        return;/*  ww w.  j  a  v  a2  s  .  c o  m*/
    }

    String username = console.readLine("Enter username: ");
    System.out.println(username);
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String fmt = "%1$4s %2$5s %3$10s%n";

    Console cnsl = System.console();

    // if console is not null
    if (cnsl != null) {

        // read line from the user input
        String alpha = cnsl.readLine(fmt, "Enter", "Alphabets: ");

        // prints
        System.out.println("Alphabets entered: " + alpha);
    }/* ww  w. j  ava  2 s  .c  o  m*/

}

From source file:MainClass.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
        System.err.println("sales: unable to obtain console");
        return;//from   w  ww .j a  v  a 2  s  .  co m
    }

    console.printf("%s ", "string");
}

From source file:MainClass.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
        System.err.println("sales: unable to obtain console");
        return;/*from  ww  w.  jav  a  2 s .co m*/
    }

    String password = new String(console.readPassword("Enter password: "));
    System.out.println(password);
}