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

public String readLine() 

Source Link

Document

Reads a single line of text from the console.

Usage

From source file:org.ow2.proactive.scheduler.authentication.ManageUsers.java

/**
 * Ask the user for parameters missing on the command line
 *///from   w w w  .  ja  va  2 s .c  o  m
private static void askInteractively(Console console, UserInfo userInfo, Action action) {
    System.out.println(action);
    System.out.print("login: ");
    if (!userInfo.isLoginSet()) {
        userInfo.setLogin(console.readLine());
    } else {
        System.out.println(userInfo.getLogin());
    }
    System.out.print("password: ");
    if ((action.isCreate() && !userInfo.isPasswordSet())
            || (action.isUpdate() && !userInfo.isPasswordSet()) && !userInfo.isGroupSet()) {
        userInfo.setPassword(new String(console.readPassword()));
    } else {
        System.out.println("*******");
    }

    if (action.isCreate() && !userInfo.isGroupSet()) {
        System.out.print("groups for user " + userInfo.getLogin() + ":");
        String groupString = console.readLine();
        userInfo.setGroups(Arrays.asList(groupString.split(",")));
    }
}

From source file:org.olamy.puzzle.robot.input.cli.RobotMoverInputBuilderCli.java

private List<String> readOrders(Console console, PrintWriter writer) {
    List<String> orders = new ArrayList<>();
    String order = console.readLine();

    while (order != null && !order.isEmpty()) {
        if (!inputValidator.validateInput(order)) {
            writer.write("Entry not correct please use a correct format");
            continue;
        }/*from  www . j  a  v  a 2 s  .c  o  m*/
        orders.add(order);
        order = console.readLine();
    }
    return orders;
}

From source file:org.ehealth_connector.demo.iti.pix.DemoMPIClient.java

private String readStringFromConsole() {
    try {/*from   ww  w.  ja  v a2  s  .  c o m*/
        Console console = System.console();
        return console.readLine();
    } catch (Exception e) {
        log.debug("note: eclipse console does not support input");
        System.out.println("aborted (eclipse console does not support input)");
        return null;
    }
}

From source file:org.olamy.puzzle.robot.input.cli.RobotMoverInputBuilderCli.java

private RobotOrder getRobotOrder(Console console, PrintWriter writer, Table table)
        throws UnknownOrientationException, OutOfTableException {
    writer.write("Robot start position and orientation (sample PLACE 1,2,EAST ) : ");
    console.flush();//from   w w w .j a v a2s . c  o m
    String robotStart = console.readLine();
    RobotOrder robotOrder = RobotOrderUtils.buildRobotOrderStart(robotStart, table);

    writer.write("Robot orders (sample RIGHT, LEFT, MOVE, REPORT ) (end input with empty line) : "
            + SystemUtils.LINE_SEPARATOR);
    console.flush();
    return robotOrder.setOrders(readOrders(console, writer));
}

From source file:org.apache.ranger.ldapconfigcheck.CommandLineOptions.java

private void readCLI() {
    boolean repeat;
    Console console = System.console();
    do {//from  www.j av a 2 s .  co m
        repeat = false;
        System.out.print("Ldap url [ldap://ldap.example.com:389]: ");
        ldapUrl = console.readLine();
        if (ldapUrl == null || ldapUrl.isEmpty()) {
            System.out.println("Please enter valid ldap url.");
            repeat = true;
        }
    } while (repeat == true);
    do {
        repeat = false;
        System.out.print("Bind DN [cn=admin,ou=users,dc=example,dc=com]: ");
        bindDn = console.readLine();
        if (bindDn == null || bindDn.isEmpty()) {
            System.out.println("Please enter valid bindDn.");
            repeat = true;
        }
    } while (repeat == true);
    do {
        repeat = false;
        System.out.print("Bind Password: ");
        char[] password = console.readPassword();
        bindPassword = String.valueOf(password);
        if (bindPassword == null || bindPassword.isEmpty()) {
            System.out.println("Bind Password can't be empty.");
            repeat = true;
        }
    } while (repeat == true);
    System.out.print("User Search Base [ou=users,dc=example,dc=com]: ");
    userSearchBase = console.readLine();
    System.out.print("User Search Filter [cn=user1]: ");
    userSearchFilter = console.readLine();

    if (isAuthEnabled) {
        do {
            repeat = false;
            System.out.print("Sample Authentication User [user1]: ");
            authUser = console.readLine();
            if (authUser == null || authUser.isEmpty()) {
                System.out.println("Sample Authentication user must not be empty!");
                repeat = true;
            }
        } while (repeat == true);
        do {
            repeat = false;
            System.out.print("Sample Authentication Password: ");
            char[] password = console.readPassword();
            authPass = String.valueOf(password);
            if (authPass == null || authPass.isEmpty()) {
                System.out.println("Sample Authentication password must not be empty!");
                repeat = true;
            }
        } while (repeat == true);
    }
}

From source file:edu.umd.cs.submit.CommandLineSubmit.java

public static String[] getSubmitUserForOpenId(String courseKey, String projectNumber, String baseURL)
        throws UnsupportedEncodingException, URISyntaxException, IOException {
    Console console = System.console();

    boolean requested = false;
    String encodedProjectNumber = URLEncoder.encode(projectNumber, "UTF-8");
    URI u = new URI(baseURL + "/view/submitStatus.jsp?courseKey=" + courseKey + "&projectNumber="
            + encodedProjectNumber);/*from  w w  w.  ja  va  2  s. c o  m*/

    if (java.awt.Desktop.isDesktopSupported()) {
        Desktop desktop = java.awt.Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            System.out.println(
                    "Your browser will connect to the submit server, which may require you to authenticate yourself");
            System.out.println("Please do so, and then you will be shown a page with a textfield on it");
            System.out.println("Then copy that text and paste it into the prompt here");
            desktop.browse(u);
            requested = true;
        }
    }
    if (!requested) {
        System.out.println("Please enter the following URL into your browser");
        System.out.println("  " + u);
        System.out.println();
        System.out.println(
                "Your browser will connect to the submit server, which may require you to authenticate yourself");
        System.out.println("Please do so, and then you will be shown a page with a textfield on it");
        System.out.println("Then copy that text and paste it into the prompt here");

    }
    while (true) {
        System.out.println();
        System.out.println("Submission verification information from browser? ");
        String info = new String(console.readLine());
        if (info.length() > 2) {
            int checksum = Integer.parseInt(info.substring(info.length() - 1), 16);
            info = info.substring(0, info.length() - 1);
            int hash = info.hashCode() & 0x0f;
            if (checksum == hash) {
                String fields[] = info.split(";");
                if (fields.length == 2) {
                    return fields;

                }
            }
        }
        System.out.println("That doesn't seem right");
        System.out.println(
                "The information should be your account name and a string of hexidecimal digits, separated by a semicolon");
        System.out.println("Please try again");
        System.out.println();
    }
}

From source file:com.act.analysis.surfactant.SurfactantLabeler.java

public void runAnalysis(String licensePath, File inputFile, File outputFile, File inchiSourceFile,
        String inchiJoinField) throws Exception {
    LicenseManager.setLicenseFile(licensePath);
    // If a separate InChI source was specified, build a map from the join key to that source's per-molecule data.
    Map<String, Map<String, String>> inchiSource = null;
    if (inchiSourceFile != null) {
        TSVParser inchiSourceParser = new TSVParser();
        inchiSourceParser.parse(inchiSourceFile);
        List<Map<String, String>> inchiSourceData = inchiSourceParser.getResults();

        inchiSource = new HashMap<>(inchiSourceData.size());
        int i = 0;
        for (Map<String, String> row : inchiSourceData) {
            i++;//from  ww  w . ja v  a 2  s  . c o m
            if (!row.containsKey(inchiJoinField)) {
                throw new RuntimeException(
                        String.format("Missing inchi join field %s on row %d of inchi source file %s",
                                inchiJoinField, i, inchiSourceFile.getAbsolutePath()));
            }
            inchiSource.put(row.get(inchiJoinField), row);
        }
        System.out.format("Loaded %d inchi source entries from %s\n", inchiSource.size(),
                inchiSourceFile.getAbsolutePath());
    }

    // Create the visualization environment.
    JFrame jFrame = new JFrame();
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    MSpaceEasy mspace = new MSpaceEasy(1, 2, true);
    mspace.addCanvas(jFrame.getContentPane());
    mspace.setSize(1200, 600);
    jFrame.pack();
    jFrame.setVisible(true);

    /* Read all the input chemicals at once.  We don't expect this file to be too big.  Do this before touching the
     * (possibly non-empty) results file in case there's a problem with the input. */
    TSVParser inputChemsParser = new TSVParser();
    inputChemsParser.parse(inputFile);
    List<Map<String, String>> chemicals = inputChemsParser.getResults();

    /* Read in any old results to find where we left off, and write them back to the (truncated) results file again.
     * TODO: add append mode to TSVWriter.  This re-writing stuff is kinda silly. */
    List<Map<String, String>> oldResults = new ArrayList<>();
    if (outputFile.exists()) {
        TSVParser oldResultsParser = new TSVParser();
        oldResultsParser.parse(outputFile);
        oldResults.addAll(oldResultsParser.getResults());
    }

    TSVWriter<String, String> resultsWriter = new TSVWriter<>(Arrays.asList("name", "id", "inchi", "label"));
    resultsWriter.open(outputFile);
    Set<String> knownInchis = new HashSet<>();
    for (Map<String, String> row : oldResults) {
        knownInchis.add(row.get("inchi"));
        resultsWriter.append(row);
    }
    resultsWriter.flush();

    int molNumber = 0;
    try {
        Console c = System.console();
        while (molNumber < chemicals.size()) {
            // Copy the source data; we're gonna add some fields to it, and a clean copy might come in handy.
            Map<String, String> r = new HashMap<>(chemicals.get(molNumber));

            // Lookup the InChI source data by this row's join field if a source is defined.
            if (inchiSource != null) {
                Map<String, String> sourceData = inchiSource.get(chemicals.get(molNumber).get(inchiJoinField));
                r.put("inchi", sourceData.get("inchi"));
                r.put("name", sourceData.get("name"));
            }

            String inchi = r.get("inchi");
            // If we've already seen this InChI, skip it and move to the next molecule.  Also de-duplicates a little bit.
            if (knownInchis.contains(inchi)) {
                System.out.format("Skpping known molecule %d\n", molNumber);
                molNumber++;
                continue;
            }
            knownInchis.add(inchi);

            /* Remove all existing molecules from the visualizer, draw the new one, and then reset/refresh to ensure both
             * panels are displayed (sometimes one of the visualizations is corrupted w/o user input due to some strange
             * mspace issue). */
            mspace.removeAllComponents();
            drawMolecule(mspace, MolImporter.importMol(inchi));
            jFrame.pack();
            mspace.resetAll();
            mspace.refresh();

            // Very liberally ask for a label.  TODO: figure out how to accept key input on the jFrame (tried+failed once).
            System.out.format("%s (%s)\n", r.get("name"), inchiJoinField == null ? "" : r.get(inchiJoinField));
            System.out.format("Input (?/1/0):\n");
            String line = c.readLine();
            String label = "?";
            switch (line) {
            case "1":
                label = "1";
                break;
            case "0":
                label = "0";
                break;
            }
            r.put("label", label);
            resultsWriter.append(r);
            // Flush every time to ensure a clean exit when the user gets tired of labeling.  It happens.
            resultsWriter.flush();

            molNumber++;
        }
    } finally {
        resultsWriter.close();
    }
    System.out.format("Done.\n");
}