Java 8 Console - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Console class.

This code example shows how to use the following methods:readLine
        theConsole.printf(format,args);
    } 
 
    @Override 
    public String readLine(String fmt, Object... args) throws IOError {
        return theConsole.readLine(fmt,args);
    } 
 
    @Override 
    public char[] readPassword(String fmt, Object... args) throws IllegalFormatException, IOError {
        return theConsole.readPassword(fmt,args);
    } 
 
    @Override 
    public Console getConsole() {
        return theConsole;
    } 
} 
7
CodeRank
This code example shows how to use the following methods:flush, writer
    public void test_getConsole() throws Exception { 
        assertNotNull(System.console());
    } 
 
    public void test_flush() { 
        console.flush();
        assertFalse(console.writer().checkError());
    } 
 
    public void test_format_LString_LObject() { 
        assertSame(console, console.format("%d %s", 1, "hello"));
        String prompt = new String(((ByteArrayOutputStream)out).toByteArray());
        assertEquals("1 hello", prompt);
    } 
 
    public void test_printf_LString_LObject() { 
        Calendar c = new GregorianCalendar(1983, 2, 21);
        assertSame(console, console.printf("%1$tm %1$te,%1$tY", c));
        String prompt = new String(((ByteArrayOutputStream)out).toByteArray());
        assertEquals("03 21,1983", prompt);
5
CodeRank
This code example shows how to use the following methods:printf
 
   @Override 
   public void execute(Console console, String argLine) {
      String[] args = argLine.split("\\s");
      if (args.length != getNumArgs() + 1) {
         console.printf("The argument list is incorrect\n");
         return; 
      } 
 
      String nameOfShares = args[1];
      try { 
         SharesUpdate sharesUpdate = (SharesUpdate) getSharesUpdateViaRest(composeKey(nameOfShares, "last"));
         if (sharesUpdate != null) {
            console.printf("------------- %s -------------\n", nameOfShares);
            console.printf("Date:  %s\n", sharesUpdate.getDate().toString());
            console.printf("Value: %f USD\n", sharesUpdate.getValue());
         } else { 
            console.printf("Last stock value for " + nameOfShares + " not found.\n");
         } 
      } catch (Exception e) {

5
CodeRank
This code example shows how to use the following methods:readLine
 
    @Override 
    public void setValue 
        (Console console)
    { 
        String value=console.readLine
            ("%s",getPrompt().getText()); //$NON-NLS-1$ 
        if ((value!=null) && !StringUtils.EMPTY.equals(value)) {
            getHolder().setValue(value);
        } 
    } 
} 
4
CodeRank
This code example shows how to use the following methods:readLine
	/** 
	 * @see org.overlord.sramp.common.shell.AbstractShellCommandReader#readLine() 
	 */ 
	@Override 
	protected String readLine() throws IOException {
		return console.readLine("s-ramp> "); //$NON-NLS-1$
	} 
 
	/** 
	 * @see org.overlord.sramp.common.shell.ShellCommandReader#close() 
	 */ 
	@Override 
	public void close() throws IOException { 
		console.flush();
		console = null;
	} 
 
	/** 
	 * @see org.overlord.sramp.shell.ShellCommandReader#promptForInput(java.lang.String) 
	 */ 
4
CodeRank
This code example shows how to use the following methods:readPassword
 
    @Override 
    public void setValue 
        (Console console)
    { 
        char[] value=console.readPassword
            ("%s",getPrompt().getText()); //$NON-NLS-1$ 
        if ((value!=null) && (value.length!=0)) {
            getHolder().setValue(value);
        } 
    } 
} 
4
CodeRank
This code example shows how to use the following methods:readPassword
	public static void main(String[] args) {
		Console cons;
		if ((cons = System.console()) != null) {
			char[] passwd = null;
			try { 
				passwd = cons.readPassword("Password:");
				// In real life you would send the password into authentication code 
				System.out.println("Your password was: " + new String(passwd));
			} finally { 
				// Shred this in-memory copy for security reasons 
				if (passwd != null) {
					java.util.Arrays.fill(passwd, ' ');
				} 
			} 
		} else { 
			throw new RuntimeException("No console, can't get password");
		} 
	} 
} 
4
CodeRank
This code example shows how to use the following methods:printf
package cz.tomascejka.app.console.command; 
 
import java.io.Console; 
 
public class CommandHelp implements Command { 
 
	public static final String KEY="help";
 
	public Object execute(final Console console) {
		console.printf("WNConsole commands:\n");
		final StringBuilder list = new StringBuilder();
		list.append("   help : ").append("Show this help").append("\n");
		list.append("  login : ").append("Allow to login in WNConsole").append("\n");
		list.append("   list : ").append("Show list all registered users from storage").append("\n");
		list.append("    add : ").append("Add user to storage").append("\n");
		list.append(" delete : ").append("Remove user from storage [only logged]").append("\n");
		list.append(" logout : ").append("Allow to logout").append("\n");
		list.append("   exit : ").append("Exit from console WNConsole").append("\n");
		console.printf(list.toString());
		return true; 

5
CodeRank
This code example shows how to use the following methods:flush, printf
        return readLine(); 
    } 
 
    @Override 
    public void printf(String format, Object... args) {
        console.printf(format, args);
        console.flush();
    } 
 
    @Override 
    public String readLine() {
        return console.readLine();
    } 
 
    @Override 
    public void putstr(String s) {
        console.writer().print(s);
        console.flush();
    } 
 
4
CodeRank
This code example shows how to use the following methods:readLine, readPassword
         
        return admin;
    } 
     
    private String readLine(String label, String defaultValue) {
        String value = console.readLine("%s [%s]:", label, defaultValue);
        return StringUtils.defaultIfEmpty(value, defaultValue);
    } 
     
    private String readEncodedPassword() {
        char[] passwordChar = console.readPassword("Password:");
        if (passwordChar.length == 0)
            throw new RuntimeException("password cannot be empty");
        String password = new String(passwordChar);
        String encodedPassword = passwordEncoder.encodePassword(password, null);
        return encodedPassword;
         
    } 
 
    public static void main(String[] args) {
5
CodeRank

Related Java 8 Console Questions & Answers:

See Code Examples for Java 8 Console Methods: