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: "******"Enter your old password: "******"Enter new password: "******"Reenter new password: "******"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);
    }

    String op = new String(oldPassword);
    System.out.println("oldPassword: "******"Unicode character at password position " + i + ": " + op.codePointAt(i));
      i++;
    }
    Arrays.fill(oldPassword, ' ');
    String op2 = new String(oldPassword);
    System.out.println("oldPassword: " + op2 + op2.length());
  }
  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");
      }
    }
  }
Exemple #3
0
 @Override
 public TextDevice printf(String fmt, Object... params) throws ConsoleException {
   console.format(fmt, params);
   return this;
 }
  @Override
  public synchronized void handle(Callback[] callbacks)
      throws IOException, UnsupportedCallbackException {
    final Console console = System.console();
    if (console == null) throw new IOException("Console not available");

    for (Callback callback : callbacks) {
      if (callback instanceof PasswordCallback) {
        final PasswordCallback password = (PasswordCallback) callback;
        final String prompt = getDefault(password.getPrompt(), "Enter password");

        char[] answer = console.readPassword("\u001b[34m%s\u001b[0m: ", prompt);
        if (answer == null) throw new EOFException();
        password.setPassword(answer);

      } else if (callback instanceof NameCallback) {
        final NameCallback name = (NameCallback) callback;
        final String prompt = getDefault(name.getPrompt(), "Enter name");
        final String defaultName = name.getDefaultName();

        if (defaultName == null)
          while (true) {
            final String answer = console.readLine("\u001b[34m%s\u001b[0m: ", prompt);
            if (answer == null) throw new EOFException();
            if (answer.length() == 0) continue;
            name.setName(answer);
            break;
          }
        else {
          final String answer =
              console.readLine(
                  "\u001b[34m%s\u001b[0m [\u001b[36m%s\u001b[0m]: ", prompt, defaultName);
          if (answer == null) throw new EOFException();
          if (answer.length() == 0) name.setName(defaultName);
          else name.setName(answer);
        }

      } else if (callback instanceof TextInputCallback) {
        final TextInputCallback input = (TextInputCallback) callback;
        final String prompt = getDefault(input.getPrompt(), "Enter value");
        final String defaultText = input.getDefaultText();

        if (defaultText == null)
          while (true) {
            final String answer = console.readLine("\u001b[34m%s\u001b[0m: ", prompt);
            if (answer == null) throw new EOFException();
            if (answer.length() == 0) continue;
            input.setText(answer);
            break;
          }
        else {
          final String answer =
              console.readLine(
                  "\u001b[34m%s\u001b[0m [\u001b[36m%s\u001b[0m]: ", prompt, defaultText);
          if (answer == null) throw new EOFException();
          if (answer.length() == 0) input.setText(defaultText);
          else input.setText(answer);
        }

      } else if (callback instanceof TextOutputCallback) {
        final TextOutputCallback output = (TextOutputCallback) callback;
        final String message = output.getMessage();
        switch (output.getMessageType()) {
          case TextOutputCallback.INFORMATION:
            console.format("\u001b[32m%s\u001b[0m\n", message);
            break;
          case TextOutputCallback.WARNING:
            console.format("\u001b[33mWARNING: %s\u001b[0m\n", message);
            break;
          case TextOutputCallback.ERROR:
            console.format("\u001b[31mERROR: %s\u001b[0m\n", message);
            break;
          default:
            console.format("\u001b[35m??? [%d]: %s\u001b[0m\n", output.getMessageType(), message);
            break;
        }

      } else {
        throw new UnsupportedCallbackException(callback, "Callback type not supported");
      }
    }
  }