Example usage for java.io Console printf

List of usage examples for java.io Console printf

Introduction

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

Prototype

public Console printf(String format, Object... args) 

Source Link

Document

A convenience method to write a formatted string to this console's output stream using the specified format string and arguments.

Usage

From source file:Main.java

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

    console.printf("%s%n", "this is a test");
}

From source file:Main.java

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

    char passwordArray[] = console.readPassword("Enter your secret password: ");
    console.printf("Password entered was: %s%n", new String(passwordArray));
}

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;/* w ww .j av a 2  s.c  om*/
    }

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

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 {/*ww  w  .j  av a2s . c om*/
        console.printf("Invalid username or password.\n");
    }
}

From source file:ch.cyberduck.cli.Terminal.java

protected static void open(final String[] args, final TerminalPreferences defaults) {
    // Register preferences
    PreferencesFactory.set(defaults);
    final Options options = TerminalOptionsBuilder.options();
    final Console console = new Console();
    try {/*from   w w w. j  av a2s.  c  o m*/
        final CommandLineParser parser = new PosixParser();
        final CommandLine input = parser.parse(options, args);
        final Terminal terminal = new Terminal(defaults, options, input);
        switch (terminal.execute()) {
        case success:
            console.printf("%s%n", StringUtils.EMPTY);
            System.exit(0);
        case failure:
            console.printf("%s%n", StringUtils.EMPTY);
            System.exit(1);
        }
    } catch (ParseException e) {
        console.printf("%s%n", e.getMessage());
        console.printf("Try '%s' for more options.%n", "duck --help");
        System.exit(1);
    } catch (FactoryException e) {
        console.printf("%s%n", e.getMessage());
        System.exit(1);
    } catch (Throwable error) {
        error.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:ch.cyberduck.cli.Terminal.java

protected Exit execute() {
    final Console console = new Console();
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override//from  w  w w .  j  a  v  a  2  s.  c  o m
        public void uncaughtException(final Thread t, final Throwable e) {
            console.printf("Uncaught failure with error message %s. Quitting application", e.getMessage());
            System.exit(1);
        }
    });
    if (input.hasOption(TerminalAction.help.name())) {
        TerminalHelpPrinter.print(options);
        return Exit.success;
    }
    if (input.hasOption(TerminalAction.version.name())) {
        TerminalVersionPrinter.print(preferences);
        return Exit.success;
    }
    if (!new TerminalOptionsInputValidator().validate(input)) {
        console.printf("Try '%s' for more options.%n", "duck --help");
        return Exit.failure;
    }
    this.configure(input);
    SessionPool source = SessionPool.DISCONNECTED;
    SessionPool destination = SessionPool.DISCONNECTED;
    try {
        final TerminalAction action = TerminalActionFinder.get(input);
        if (null == action) {
            return Exit.failure;
        }
        final String uri = input.getOptionValue(action.name());
        final Host host = new CommandLineUriParser(input).parse(uri);
        final LoginConnectionService connect = new LoginConnectionService(
                new TerminalLoginService(input, new TerminalLoginCallback(reader)),
                new TerminalHostKeyVerifier(reader), progress);
        source = SessionPoolFactory.create(connect, transcript, cache, host,
                new CertificateStoreX509TrustManager(new DefaultTrustManagerHostnameCallback(host),
                        new TerminalCertificateStore(reader)),
                new PreferencesX509KeyManager(host, new TerminalCertificateStore(reader)),
                VaultRegistryFactory.create(new TerminalPasswordCallback()));
        final Path remote;
        if (new CommandLinePathParser(input).parse(uri).getAbsolute().startsWith(TildePathExpander.PREFIX)) {
            final Home home = source.getFeature(Home.class);
            remote = new TildePathExpander(home.find()).expand(new CommandLinePathParser(input).parse(uri));
        } else {
            remote = new CommandLinePathParser(input).parse(uri);
        }
        switch (action) {
        case edit:
            return this.edit(source, remote);
        case list:
        case longlist:
            return this.list(source, remote, input.hasOption(TerminalOptionsBuilder.Params.longlist.name()));
        case mount:
            return this.mount(source);
        case delete:
            return this.delete(source, remote);
        }
        switch (action) {
        case download:
        case upload:
        case synchronize:
            return this.transfer(
                    new TerminalTransferFactory().create(input, host, remote,
                            new ArrayList<TransferItem>(
                                    new SingleTransferItemFinder().find(input, action, remote))),
                    source, SessionPool.DISCONNECTED);
        case copy:
            final Host target = new CommandLineUriParser(input).parse(input.getOptionValues(action.name())[1]);
            destination = SessionPoolFactory.create(connect, transcript, cache, target,
                    new CertificateStoreX509TrustManager(new DefaultTrustManagerHostnameCallback(target),
                            new TerminalCertificateStore(reader)),
                    new PreferencesX509KeyManager(target, new TerminalCertificateStore(reader)),
                    VaultRegistryFactory.create(new TerminalPasswordCallback()));
            return this.transfer(
                    new CopyTransfer(host, target,
                            Collections.singletonMap(remote,
                                    new CommandLinePathParser(input)
                                            .parse(input.getOptionValues(action.name())[1]))),
                    source, destination);
        default:
            throw new BackgroundException(LocaleFactory.localizedString("Unknown"),
                    String.format("Unknown transfer type %s", action.name()));
        }
    } catch (ConnectionCanceledException e) {
        log.warn("Connection canceled", e);
        return Exit.success;
    } catch (BackgroundException e) {
        final StringAppender b = new StringAppender();
        b.append(e.getMessage());
        b.append(e.getDetail());
        console.printf("%n%s", b.toString());
    } finally {
        this.disconnect(source);
        this.disconnect(destination);
        console.printf("%n%s", StringUtils.EMPTY);
    }
    return Exit.failure;
}