This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
java:console_read [2015/06/20 02:29] gthanos [Διαβάζοντας από το System.in] |
java:console_read [2015/06/20 02:38] gthanos [Διαβάζοντας με χρήση της κλάσης Console] |
||
|---|---|---|---|
| Line 58: | Line 58: | ||
| ===== Διαβάζοντας με χρήση της κλάσης Console ===== | ===== Διαβάζοντας με χρήση της κλάσης Console ===== | ||
| + | |||
| + | Μπορείτε να διαβάσετε ή να γράψετε στην κονσόλα με την βοήθεια της κλάσης [[http:// | ||
| + | |||
| + | <code java Password.java> | ||
| + | import java.io.Console; | ||
| + | import java.util.Arrays; | ||
| + | import java.io.IOException; | ||
| + | |||
| + | public class Password { | ||
| + | | ||
| + | public static void main (String args[]) throws IOException { | ||
| + | |||
| + | Console c = System.console(); | ||
| + | if (c == null) { | ||
| + | System.err.println(" | ||
| + | System.exit(1); | ||
| + | } | ||
| + | |||
| + | String login = c.readLine(" | ||
| + | char [] oldPassword = c.readPassword(" | ||
| + | |||
| + | if (verify(login, | ||
| + | boolean noMatch; | ||
| + | do { | ||
| + | char [] newPassword1 = c.readPassword(" | ||
| + | char [] newPassword2 = c.readPassword(" | ||
| + | noMatch = ! Arrays.equals(newPassword1, | ||
| + | if (noMatch) { | ||
| + | c.format(" | ||
| + | } else { | ||
| + | change(login, | ||
| + | c.format(" | ||
| + | } | ||
| + | Arrays.fill(newPassword1, | ||
| + | Arrays.fill(newPassword2, | ||
| + | } while (noMatch); | ||
| + | } | ||
| + | |||
| + | Arrays.fill(oldPassword, | ||
| + | } | ||
| + | | ||
| + | // Dummy change method. | ||
| + | static boolean verify(String login, char[] password) { | ||
| + | // This method always returns | ||
| + | // true in this example. | ||
| + | // Modify this method to verify | ||
| + | // password according to your rules. | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | // Dummy change method. | ||
| + | static void change(String login, char[] password) { | ||
| + | // Modify this method to change | ||
| + | // password according to your rules. | ||
| + | } | ||
| + | } | ||
| + | </ | ||