Guest User

Untitled

a guest
Feb 10th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.io.Console;
  3.  
  4. /**
  5. * Simple interactive console application.
  6. * Uses the java.io.Console class of Java 6.
  7. */
  8. public final class Console6 {
  9.  
  10. public static final void main(String... aArgs){
  11. Console console = System.console();
  12. //read user name, using java.util.Formatter syntax :
  13. String username = console.readLine("User Name? ");
  14.  
  15. //read the password, without echoing the output
  16. char[] password = console.readPassword("Password? ");
  17.  
  18. //verify user name and password using some mechanism (elided)
  19.  
  20. //the javadoc for the Console class recommends "zeroing-out" the password
  21. //when finished verifying it :
  22. Arrays.fill(password, ' ');
  23.  
  24. console.printf("Welcome, %1$s.", username);
  25. console.printf(fNEW_LINE);
  26.  
  27. String className = console.readLine("Please enter a package-qualified class name:");
  28. Class theClass = null;
  29. try {
  30. theClass = Class.forName(className);
  31. console.printf("The inheritance tree: %1$s", getInheritanceTree(theClass));
  32. }
  33. catch(ClassNotFoundException ex){
  34. console.printf("Cannot find that class.");
  35. }
  36.  
  37. //this version just exits, without asking the user for more input
  38. console.printf("Bye.");
  39. }
  40.  
  41. // PRIVATE
  42. private static final String fNEW_LINE = System.getProperty("line.separator");
  43.  
  44. private static String getInheritanceTree(Class aClass){
  45. StringBuilder superclasses = new StringBuilder();
  46. superclasses.append(fNEW_LINE);
  47. Class theClass = aClass;
  48. while (theClass != null) {
  49. superclasses.append(theClass);
  50. superclasses.append(fNEW_LINE);
  51. theClass = theClass.getSuperclass();
  52. }
  53. superclasses.append(fNEW_LINE);
  54. return superclasses.toString();
  55. }
  56. }
Add Comment
Please, Sign In to add comment