I tried to add codes to validate the input in the Exercise 3 of JAVA EXCEPTIONS AND ASSERTION.
My purpose was to catch error for ALPHABETIC inputs. I'm confused why instead of executing the codes to catch NumberFormatException and then display the message "Input should be numeric" on JoptionPane,  JAVA normal runtime error was displayed.

This was the runtime error after inputting ALPHABETIC characters.
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:447)
        at java.lang.Integer.parseInt(Integer.java:497)
        at assertexample.Diamond.main(Main.java:43)


These are the codes I added:
catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Input should be numeric. ");


Below is the complete codes.

Any help would be appreciated.

Ray


package assertexample;

import javax.swing.*;

class Diamond {

    static void printDiamond(int size) {
        String diamond = "";
        /* print upper triangle */
        for (int r = 1, a = 1; r <= size; r++, a+=2) {
            /* print spaces */
            for (int i = size - r; i >= 1; i--) {
                diamond += " ";
            }
            /* print *'s */
            for (int j = 1; j <= a; j++) {
                diamond += "*";
            }
            diamond += "\n";
        }
        /* print lower triangle */
        for (int r = size - 1, a = 2*(size-1)-1; r >= 1; r--, a-=2) {
            /* print spaces */
            for (int i = size - r; i >= 1; i--) {
                diamond += " ";
            }
            /* print *'s */
            for (int j = 1; j <= a; j++) {
                diamond += "*";
            }
            diamond += "\n";
        }
        JOptionPane.showMessageDialog(null, diamond);
        System.out.println(diamond);
    }

    public static void main(String args[]) {
        String strSize;

        // Get the size of the diamond to draw
        strSize = JOptionPane.showInputDialog(null, "Enter diamond size:");
        int size = Integer.parseInt(strSize);

        // Assert that the value entered is greater than 0, otherwise,
        // it will generate AssertionError exception.
        try {
            assert(size > 0);
            printDiamond(size);
        } catch (AssertionError e) {
            JOptionPane.showMessageDialog(null, "AssertionError is captured: " +
                    "Size should be > 0.");
        }
         catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Input should be numeric. ");
                            }

        System.exit(0);
    }
}


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to javaprogrammingwithpassion-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to