Results 1 to 2 of 2
  1. #1
    beastkat is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default Throws/Exceptions not working properly

    I'm working on the project and cannot get my catch and throws to execute correctly. If I input numbers in both fields (timeIn and paymentIn), it gives me last error message but I need to be able to enter in a number in the timeIn or paymentIn field and get the error message. i'm using netbeans and I'm new to the Java world. The below is what I think the issue is in and everything else is working fine:

    Java Code:
    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            //Get input (minutes / payment) from user
     
                timeIn = Double.parseDouble(minutesField.getText());
                paymentIn = Double.parseDouble(paymentField.getText());
             try 
             {    
                if (timeIn <= 0 ) {
                     throw new IllegalArgumentException();
                 }
             }
             catch(IllegalArgumentException exception)
             {
                 System.out.println("Enter higher number");
                 return;
             }
             try 
             {    
                if (timeIn > 240 ) {
                     throw new IllegalArgumentException();
                 }
             }
             catch(IllegalArgumentException exception)
             {
                 System.out.println("Number must be under 240");
                 return;
             }
             
             try 
             {    
                if (paymentIn <= 0 ) {
                     throw new IllegalArgumentException();
                 }
             }
             catch(IllegalArgumentException exception)
             {
                 System.out.println("Enter higher number");
                 return;
             }
            // Store input into array
            //double[][] userInput = {{timeIn, paymentIn}};
            //double[][] userInput = new double[20][2];
            //userInput [MAX_ROWS][COLUMNS] = timeIn ;
             
            System.out.println(userInput[0][0] + "\t" + (userInput[0][1]));
    Last edited by pbrockway2; 02-05-2013 at 05:14 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Throws/Exceptions not working properly

    Hi beastkat, welcome to the forums! I've added "code" tags to your post: when you post code put [code] at the start of the code and [/code] at the end. That way the forum software will present the code in a way that's readable.

    ---

    In general where you have

    Java Code:
    try 
    {    
        if (someCondition) {
            throw new SomeException();
        }
    }
    catch(SomeException exception)
    {
        System.out.println(message);
        return;
    }
    I would write

    Java Code:
    if(someCondition) {
        System.out.println(message);
        return;
    }
    Less - and simpler - code, the better!

    One sort of place where try/catch might be used even though the situation is only barely "exceptional" is the parseDouble() methods that you use. If I understanding you correctly, you want some sort of message output if the user leaves one or other of the text fields blank. What does getText() return in such a case? And what does parseDouble() do?

    It turns out that blank (or "junk") text fields will result in an exception and *that* one should be caught and the error output printed.

Similar Threads

  1. iterator not working properly
    By arieltal in forum Advanced Java
    Replies: 3
    Last Post: 09-29-2012, 01:01 PM
  2. NetBeans IDE not working properly
    By farmer in forum NetBeans
    Replies: 2
    Last Post: 12-09-2011, 03:12 PM
  3. TCP/IP client not working properly
    By Chrillz in forum Networking
    Replies: 2
    Last Post: 07-20-2011, 07:39 PM
  4. Applet throws exceptions
    By demaja in forum New To Java
    Replies: 4
    Last Post: 05-09-2011, 02:57 PM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •