Example usage for java.io Console readline

List of usage examples for java.io Console readline

Introduction

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

Prototype

private char[] readline(boolean zeroOut) throws IOException 

Source Link

Usage

From source file:Login.java

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

    System.out.println("Username = " + username);
    System.out.println("Password = " + new String(pwd));

    username = "";
    for (int i = 0; i < pwd.length; i++)
        pwd[i] = 0;/*from   w  w w  . ja v a 2  s. co m*/
}

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 va  2 s.c  om
        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) {

        String name = cnsl.readLine("Name: ");

        System.out.println("Name entered : " + name);
    }/*from   w w w. j a  v  a 2 s  .  c  om*/

}

From source file:Main.java

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

    Console cnsl = System.console();

    if (cnsl != null) {

        String alpha = cnsl.readLine("Name: ");

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

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

        System.out.println("Password is: " + pwd);
    }/*www .  j  a v a 2s. co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    Pattern pattern = null;//from  ww  w  .j a  v  a 2s .  c  o  m
    Matcher matcher = null;

    Console console = System.console();
    while (true) {
        try {
            pattern = Pattern.compile(console.readLine("%nEnter your regex: "));

            matcher = pattern.matcher(console.readLine("Enter input string to search: "));
        } catch (PatternSyntaxException pse) {
            console.format("There is a problem with the regular expression!%n");
            console.format("The pattern in question is: %s%n", pse.getPattern());
            console.format("The description is: %s%n", pse.getDescription());
            console.format("The message is: %s%n", pse.getMessage());
            console.format("The index is: %s%n", pse.getIndex());
            System.exit(0);
        }
        boolean found = false;
        while (matcher.find()) {
            console.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
            found = true;
        }
        if (!found) {
            console.format("No match found.%n");
        }
    }
}

From source file:Main.java

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

    Console cnsl = System.console();

    // test for console not null
    if (cnsl != null) {

        // read line from the console
        String name = cnsl.readLine("Enter  name  : ");

        // print/*from  w w w.ja va  2s  . c o  m*/
        System.out.println("You have entered : " + name);
    }

    // flushes console and forces output to be written
    cnsl.flush();

}

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;/*  w w w  .jav  a 2s  .  c  o  m*/
    }

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

From source file:Password.java

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

    Console c = System.console();
    if (c == null) {
        System.err.println("No console.");
        System.exit(1);//from  w  w w  .j a va 2 s  . c o  m
    }

    String login = c.readLine("Enter your login: ");
    char[] oldPassword = c.readPassword("Enter your old password: ");

    if (verify(login, oldPassword)) {
        boolean noMatch;
        do {
            char[] newPassword1 = c.readPassword("Enter your new password: ");
            char[] newPassword2 = c.readPassword("Enter new password again: ");
            noMatch = !Arrays.equals(newPassword1, newPassword2);
            if (noMatch) {
                c.format("Passwords don't match. Try again.%n");
            } else {
                change(login, newPassword1);
                c.format("Password for %s changed.%n", login);
            }
            Arrays.fill(newPassword1, ' ');
            Arrays.fill(newPassword2, ' ');
        } while (noMatch);
    }

    Arrays.fill(oldPassword, ' ');

}

From source file:Main.java

public static void main(String[] args) {
    Console console = System.console();
    if (console != null) {
        console.printf("Console is  available.%n");
    } else {//from  w ww .  j  ava 2 s. c  om
        System.out.println("Console is  not  available.%n");
        return; // A console is not available
    }
    String userName = console.readLine("User Name: ");
    char[] passChars = console.readPassword("Password: ");
    String passString = new String(passChars);
    if (passString.equals("letmein")) {
        console.printf("Hello %s", userName);
    } else {
        console.printf("Invalid  password");
    }
}

From source file:org.jsweet.candies.InitProjectTool.java

public static void main(String[] args) throws Throwable {
    logger.info("JSweet init candy project tool - version: " + JSweetDefTranslatorConfig.getVersionNumber());

    JSAP jsapSpec = defineArgs();//from w ww .  j av  a  2s .c om
    JSAPResult jsapArgs = parseArgs(jsapSpec, args);
    if (!jsapArgs.success() || jsapArgs.getBoolean("help")) {
        printUsage(jsapSpec);
        System.exit(0);
    }
    if (jsapArgs.getBoolean("verbose")) {
        LogManager.getLogger("org.jsweet").setLevel(Level.ALL);
    }

    String artifactId = jsapArgs.getString("artifactId");
    String version = jsapArgs.getString("version");

    List<String> dependencyLocators = asList(defaultString(jsapArgs.getString("deps")).split(","));

    File outDir = jsapArgs.getFile("out");
    if (outDir == null || StringUtils.isBlank(outDir.getPath())) {
        outDir = new File("./target/candy-projects");
    }
    outDir.mkdirs();

    String projectName = "candy-" + artifactId;
    File projectDir = new File(outDir, projectName);

    logger.info("init candy project: \n" //
            + "* projectName: " + projectName + "\n" //
            + "* artifactId: " + artifactId + "\n" //
            + "* version: " + version + "\n" //
            + " to: " + projectDir.getAbsolutePath());

    FileUtils.copyDirectory(getResourceFile("templates/candy-project"), projectDir);

    logger.info("generating README");
    File readmeFile = new File(projectDir, "README.md");
    String readmeFileContent = FileUtils.readFileToString(readmeFile) //
            .replace("${{CANDY_NAME}}", artifactId).replace("${{CANDY_VERSION}}", version);
    FileUtils.write(readmeFile, readmeFileContent);

    logger.info("generating pom.xml");
    File pomFile = new File(projectDir, "pom.xml");
    String pomFileContent = FileUtils.readFileToString(pomFile) //
            .replace("${{ARTIFACT_ID}}", artifactId) //
            .replace("${{VERSION}}", version) //
            .replace("${{DEPENDENCIES}}", generateMavenXmlForDependencies(dependencyLocators));
    FileUtils.write(pomFile, pomFileContent);

    if (BooleanUtils.toBoolean(jsapArgs.getString("createGitHubRepository"))) {
        String gitHubUser = jsapArgs.getString("gitHubUser");
        String gitHubPass = jsapArgs.getString("gitHubPass");

        Console console = System.console();
        if (isBlank(gitHubUser)) {
            gitHubUser = console.readLine("GitHub username: ");
        }

        if (isBlank(gitHubPass)) {
            gitHubPass = new String(console.readPassword("GitHub password for " + gitHubUser + ": "));
        }

        createGitHubRepo(projectName, "Java API bridge for " + artifactId + " (JSweet candy)", gitHubUser,
                gitHubPass);

        ProcessUtil.init();
        ProcessUtil.runCmd(projectDir, (out) -> {
            logger.info("git: " + out);
        }, "git init && git remote add origin https://github.com/jsweet-candies/" + projectName);
    }

    logger.info("***************************************************************************");
    logger.info("candy project " + projectName + " successfully created to " + projectDir);
    logger.info("***************************************************************************");
}