本文整理匯總了Java中java.io.Console.format方法的典型用法代碼示例。如果您正苦於以下問題:Java Console.format方法的具體用法?Java Console.format怎麽用?Java Console.format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.io.Console
的用法示例。
在下文中一共展示了Console.format方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readPasswordFromConsole
import java.io.Console; //導入方法依賴的package包/類
/**
* Prompts the user for the password on the console
*
* @param user
* the username
*
* @return the password
*
* @throws Exception
*/
private static String readPasswordFromConsole(String user) throws Exception {
Console c = System.console();
String password = "";
if (c == null) {
System.err.println("No console.");
System.out.println(String.format("You are logging in as: " + user + "\n"));
System.out.print(String.format("Enter the password of that user account: "));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
password = br.readLine();
System.out.println("");
}
else {
c.format("You are logging in as: " + user + "\n");
char[] passwordChar = c.readPassword("Enter the password of that user account: ");
password = new String(passwordChar);
System.out.println("");
}
return new String(password);
}
示例2: run
import java.io.Console; //導入方法依賴的package包/類
@Override
public void run() {
try {
CONSOLE_LOCK.lockInterruptibly();
} catch (InterruptedException e) {
return; // don't need to login anymore -- promise was cancelled
}
try {
String user = factoryUser;
String password = factoryPassword;
Console console = System.console();
if (console == null) {
Scanner scanner = new Scanner(System.in);
System.out.println(reason);
if (user.isEmpty()) {
System.out.print(USER);
user = scanner.nextLine();
}
if (password.isEmpty()) {
System.out.print(PASSWORD);
password = scanner.nextLine();
}
} else {
console.format("%s%n", reason);
if (user.isEmpty())
user = console.readLine(USER);
if (password.isEmpty())
password = String.valueOf(console.readPassword(PASSWORD));
}
done(AuthToken.createBasicToken(user, password));
} finally {
CONSOLE_LOCK.unlock();
}
}
示例3: main
import java.io.Console; //導入方法依賴的package包/類
public static void main( String[] args ) {
String nome = "";
Console c = System.console();
char[] senha = c.readPassword();
for ( char ch : senha ) {
c.format( "%c" , ch );
}
c.format( "\n" );
while ( true ) {
nome = c.readLine( "%s" , "entrada?: " );
c.format( "saida: %s \n" , fazerAlgo( nome ) );
}
}
示例4: waitForEnter
import java.io.Console; //導入方法依賴的package包/類
private static void waitForEnter() {
Console c = System.console();
if (c != null) {
c.format("\nPress ENTER to proceed.\n");
c.readLine();
}
}
示例5: waitForEnter
import java.io.Console; //導入方法依賴的package包/類
public static void waitForEnter() {
Console c = System.console();
if (c != null) {
c.format("\nPress ENTER to proceed.\n");
c.readLine();
}
}
示例6: main
import java.io.Console; //導入方法依賴的package包/類
public static void main(String[] args){
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern =
Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher =
pattern.matcher(console.readLine("Enter input string to search: "));
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");
}
}
}
示例7: main
import java.io.Console; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
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 your 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);
}
}
示例8: main
import java.io.Console; //導入方法依賴的package包/類
public static void main(String[] args) {
Pattern pattern = null;
Matcher matcher = null;
Console console = System.console();
if (console == null) {
System.err.println("No console");
System.exit(1);
}
while (true) {
try {
pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
matcher = pattern.matcher(console.readLine("%nEnter input sting to search: "));
} catch (PatternSyntaxException e) {
console.format("There is a problem with the regular expression!%n");
console.format("The description is: %s%n", e.getDescription());
console.format("The message is: %s%n", e.getMessage());
console.format("The index is: %s%n", e.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");
}
}
}
示例9: main
import java.io.Console; //導入方法依賴的package包/類
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));
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");
}
}
}
示例10: main
import java.io.Console; //導入方法依賴的package包/類
public static void main (String args[]) throws IOException {
Console c = System.console(); // Console object is retrieved
if (c == null) { // can be null if program is launched in a non-interactive environment
System.err.println("No console.");
System.exit(1);
}
String login = c.readLine("Enter your login: "); // prompt for the username
char [] oldPassword = c.readPassword("Enter your old password: "); // doesn't display characters and returns a char array (not a string)
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, ' '); // Overwrite both passwords with blanks.
Arrays.fill(newPassword2, ' '); // Overwrite both passwords with blanks.
} while (noMatch);
}
Arrays.fill(oldPassword, ' '); // Overwrite both passwords with blanks.
}
示例11: waitForEnter
import java.io.Console; //導入方法依賴的package包/類
private static void waitForEnter() {
Console c = System.console();
if (c != null) {
c.format("\nPress ENTER to proceed.\n");
c.readLine();
}
}
示例12: main
import java.io.Console; //導入方法依賴的package包/類
public static void main(String... args){
Pattern pattern = null;
Matcher matcher = null;
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
try {
pattern = Pattern.compile(console.readLine("%n", "Enter your regex: "));
matcher = pattern.matcher(console.readLine("%n", "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");
}
}
}
示例13: format
import java.io.Console; //導入方法依賴的package包/類
public void format(String message) {
Console console = System.console();
console.format(message);
}
示例14: main
import java.io.Console; //導入方法依賴的package包/類
public static void main(String ... p) {
Formatter f = new Formatter();
f.format("%d", 1337);
f.format(Locale.GERMAN, "%d", 1337);
//:: error: (argument.type.incompatible)
f.format("%f", 1337);
//:: error: (argument.type.incompatible)
f.format(Locale.GERMAN, "%f", 1337);
f.close();
String.format("%d", 1337);
String.format(Locale.GERMAN, "%d", 1337);
//:: error: (argument.type.incompatible)
String.format("%f", 1337);
//:: error: (argument.type.incompatible)
String.format(Locale.GERMAN, "%f", 1337);
PrintWriter pw = new PrintWriter(new ByteArrayOutputStream());
pw.format("%d", 1337);
pw.format(Locale.GERMAN, "%d", 1337);
pw.printf("%d", 1337);
pw.printf(Locale.GERMAN, "%d", 1337);
//:: error: (argument.type.incompatible)
pw.format("%f", 1337);
//:: error: (argument.type.incompatible)
pw.format(Locale.GERMAN, "%f", 1337);
//:: error: (argument.type.incompatible)
pw.printf("%f", 1337);
//:: error: (argument.type.incompatible)
pw.printf(Locale.GERMAN, "%f", 1337);
pw.close();
PrintStream ps = System.out;
ps.format("%d", 1337);
ps.format(Locale.GERMAN, "%d", 1337);
ps.printf("%d", 1337);
ps.printf(Locale.GERMAN, "%d", 1337);
//:: error: (argument.type.incompatible)
ps.format("%f", 1337);
//:: error: (argument.type.incompatible)
ps.format(Locale.GERMAN, "%f", 1337);
//:: error: (argument.type.incompatible)
ps.printf("%f", 1337);
//:: error: (argument.type.incompatible)
ps.printf(Locale.GERMAN, "%f", 1337);
ps.close();
Console c = System.console();
c.format("%d", 1337);
c.printf("%d", 1337);
//:: error: (argument.type.incompatible)
c.format("%f", 1337);
//:: error: (argument.type.incompatible)
c.printf("%f", 1337);
}
示例15: main
import java.io.Console; //導入方法依賴的package包/類
/**
* Sample program to test pattern matching.
*
* @param args command line arguments
* @throws BeliefBaseException thrown if something went wrong
*/
public static void main(String[] args) throws BeliefBaseException {
BeliefBase bb = new ABeliefStore(100, 4);
bb.eval(0, "neighbour.age < 31");
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));
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");
}
}
}