Showing posts with label Console. Show all posts
Showing posts with label Console. Show all posts

Friday, May 16, 2014

NetBeans - Throwable.printStackTrace() should be removed

If you use NetBeans for your Java development, whenever you use printStackTrace() in a catch block, you get a warning sign that the Throwable.printStackTrace() should be removed.

To get rid of this warning sign, do one of the followings

         1. Log the exception. For example to log to a file.
                  Logger logger = Logger. getAnonymousLogger();
                  logger.addHandler(new FileHandler("<directory>/<file name>");
                  logger.log(Level.SEVERE, <exception>.getMessage(), <exception>);
     
         2. Print the stack trace to a file
                  PrintStream ps = new PrintStream(
                                            new FileOutputStream("<directory>/<file name>", true));
                  <exception>.printStackTrace(ps);

         3. If your really want to print to the console, use <exception>.printStackTrace(System.out) instead of <exception>.printStackTrace()
           
--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Tuesday, May 6, 2014

Read from and write to the command line in Java

A. Read from the command line

1. The Scanner
          Scanner theScanner = new Scanner(System.in);
          if (theScanner.hasNextLine()) {
                     String input = theScanner.nextLine();
           }

2. The BufferedReader and the InputStreamReader
           BufferedReader theReader = new BufferedReader(
                                  new InputStreamReader(System.in));
           String line = theReader.readLine();

3. The Console
          Console theConsole = System.Console();
          if (theConsole != null) {
                    String line = theConsole.readLine();
                    char[] password = theConsole.readPassword("Enter your password: ");
                    ............
                    Arrays.fill(password, ' ');
          }

B. Write to the command line

1. The System.out
           System.out.println("The word to write");

2. The Console
           Console theConsole = System.Console();
           if (theConsole != null) {
                      theConsole.format("Enter 1 to continue, 2 to exit");
           }

References:

1. I/O from the Command Line

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.