Example usage for java.io Console reader

List of usage examples for java.io Console reader

Introduction

In this page you can find the example usage for java.io Console reader.

Prototype

Reader reader

To view the source code for java.io Console reader.

Click Source Link

Usage

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);
        }//w w w  . j  a  va2s  . com
    }
}

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);/*ww  w .ja  v  a 2s  .  c o 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:org.xlrnet.tibaija.Application.java

private static VirtualCalculator getDefaultCalculator(CodeProvider codeProvider) throws IOException {
    Reader reader;/*from   w  w w . ja v  a 2s  .co  m*/
    Writer writer;

    if (System.console() != null) {
        Console console = System.console();
        reader = console.reader();
        writer = console.writer();
        LOGGER.debug("Initialised native system console");
    } else {
        reader = new InputStreamReader(System.in);
        writer = new OutputStreamWriter(System.out);
        LOGGER.debug("Initialised system I/O streams");
    }

    CalculatorIO io = new ConsoleIO(reader, writer);
    CalculatorMemory memory = new DefaultCalculatorMemory();
    HomeScreen homeScreen = new NullHomeScreen();
    FontRegistry fontRegistry = new FontRegistry();
    fontRegistry.registerFont(Paths.get("largeFont.json"), FontConstants.FONT_LARGE);
    fontRegistry.registerFont(Paths.get("smallFont.json"), FontConstants.FONT_SMALL);

    ExecutionEnvironment.newEnvironment(memory, io, codeProvider, homeScreen, fontRegistry);
    return new TI83Plus(memory, io, codeProvider);
}

From source file:pa55.java.core.PA55.java

public static void main(String[] args)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
    System.out.println("**** This is a reference implementation version " + APP_VERSION
            + " of pa55. Please see the project page (http://anirbanbasu.github.io/pa55/) for details. ****\r\n");
    Console inputConsole = System.console(); //If it is available, we need Console for masking passwords.
    Scanner inputConsoleScanner;/*from w  ww. ja v  a2s  . co m*/
    Scanner echoOptionScanner = new Scanner(System.in);
    boolean disableEcho = false;
    if (inputConsole != null) {
        //We can hide the input, but should we? Ask the user.
        System.out.println(
                "Do you want to hide the master secret and the password hint as you type them? Enter 1 to hide or 0 to not hide.");
        disableEcho = echoOptionScanner.nextInt() == 0 ? false : true;
    }
    String echoNotice;
    if (disableEcho) {
        inputConsoleScanner = new Scanner(inputConsole.reader());
        echoNotice = " (your input will NOT be visible as you type)";
    } else {
        inputConsoleScanner = new Scanner(System.in);
        echoNotice = " (your input will be visible as you type)";
    }
    PA55 core = new PA55();
    String lineInput = "";
    System.out.println("Enter master secret" + echoNotice);
    lineInput = (disableEcho ? new String(inputConsole.readPassword()) : inputConsoleScanner.nextLine().trim());
    while (lineInput.length() == 0) {
        System.out.println("Please enter a non-empty string" + echoNotice);
        lineInput = (inputConsole != null ? new String(inputConsole.readPassword())
                : inputConsoleScanner.nextLine().trim());
    }
    core.setMasterSecret(lineInput);
    System.out.println("Enter password hint" + echoNotice);
    lineInput = (disableEcho ? new String(inputConsole.readPassword()) : inputConsoleScanner.nextLine().trim());
    while (lineInput.length() == 0) {
        System.out.println("Please enter a non-empty string" + echoNotice);
        lineInput = (inputConsole != null ? new String(inputConsole.readPassword())
                : inputConsoleScanner.nextLine().trim());
    }
    core.setPasswordHint(lineInput);
    int choiceInput = 0;
    System.out.println(
            "Choose desired length in characters:\r\n (0) default: 12 characters\r\n (1) 8 characters\r\n (2) 12 characters\r\n (3) 16 characters\r\n (4) 20 characters\r\n (5) 24 characters\r\n (6) 28 characters\r\n (7) 32 characters");
    choiceInput = inputConsoleScanner.nextInt();
    while (choiceInput < 0 || choiceInput > 7) {
        System.out.println("Please enter a choice between 0 and 7.");
        choiceInput = inputConsoleScanner.nextInt();
    }
    if (choiceInput == 0) {
        choiceInput = 2;
    }
    core.setPbkdfLength((choiceInput + 1) * 3);
    System.out.println(
            "Choose the password iterations:\r\n (0) default: 500K\r\n (1) Low (10K)\r\n (2) 250K\r\n (3) 500K\r\n (4) 750K\r\n (5) 1M\r\n (6) 1.25M\r\n (7) 1.5M");
    choiceInput = inputConsoleScanner.nextInt();
    while (choiceInput < 0 || choiceInput > 7) {
        System.out.println("Please enter a choice between 0 and 7.");
        choiceInput = inputConsoleScanner.nextInt();
    }
    if (choiceInput == 0) {
        choiceInput = 3;
    }
    if (choiceInput != 1) {
        core.setPbkdfRounds((choiceInput - 1) * 250000);
    } else {
        core.setPbkdfRounds(10000);
    }
    System.out.println(
            "Choose the HMAC algorithm:\r\n (0) default: SHA256\r\n (1) SHA1\r\n (2) SHA256\r\n (3) SHA512");
    choiceInput = inputConsoleScanner.nextInt();
    while (choiceInput < 0 || choiceInput > 3) {
        System.out.println("Please enter a choice between 0 and 3.");
        choiceInput = inputConsoleScanner.nextInt();
    }
    if (choiceInput == 0) {
        choiceInput = 2;
    }
    core.setPbkdfAlgorithm(HMACHashFunction.values()[choiceInput - 1]);
    inputConsoleScanner.close();
    echoOptionScanner.close();
    System.out.print("Generating password...\r");
    core.generatePBKDF2Password();
    System.out.println("Your password is: " + core.getPbkdfGeneratedPassword());
}