Announcement

Collapse
No announcement yet.

Programming question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #91
    Ok, I'm done the program now. Only thing left that I want to do is flash up a window on my exceptions stating the error. So lets say someone enters a negative number for their payment in the payment field and they hit enter I want a window to flash up saying "Invalid payment amount. Must be greater than zero." Any idea on how to do this? Thanks.

    Comment


    • #92
      Boring. Give them an interesting message like: "WTF! You can't make a negative payment. ****."
      “As a lifelong member of the Columbia Business School community, I adhere to the principles of truth, integrity, and respect. I will not lie, cheat, steal, or tolerate those who do.”
      "Capitalism ho!"

      Comment


      • #93
        Originally posted by DriXnaK View Post
        Ok, I'm done the program now. Only thing left that I want to do is flash up a window on my exceptions stating the error. So lets say someone enters a negative number for their payment in the payment field and they hit enter I want a window to flash up saying "Invalid payment amount. Must be greater than zero." Any idea on how to do this? Thanks.
        Step 1: read the spec of Double.parseDouble() (http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/lang/Double.html)

        Step 2:

        Code:
        try {
            //code that could throw an exception goes here
        } catch (AppropriateExceptionType e) {
            //code to handle exception, should it occur, goes here
        }

        Comment


        • #94
          Ok, I'm lost. Spent probably an hour now maybe trying to figure this out. Here's my latest attempt.

          try {
          Double.parseDouble(payment.getText());
          }
          catch (IllegalArgumentException exception){
          if (Double.parseDouble(payment.getText()) < 0);
          System.out.println("Invalid payment amount.");
          }

          This one down below is my original exception that worked but didn't tell the user that there had been an invalid payment amount entered:

          if (paymentValue < 0)
          {
          IllegalArgumentException exception
          = new IllegalArgumentException("Invalid payment amount.");
          throw exception;
          }
          Last edited by DriXnaK; October 20, 2010, 16:44.

          Comment


          • #95
            Code:
            parseDouble
            
            public static double parseDouble(String s)
                                      throws NumberFormatException
            
                Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
            
                Parameters:
                    s - the string to be parsed. 
                Returns:
                    the double value represented by the string argument. 
                Throws:
                    NumberFormatException - if the string does not contain a parsable double.
                Since:
                    1.2
                See Also:
                    valueOf(String)
            Double.parseDouble() doesn't throw IllegalArgumentException.

            Comment


            • #96
              I tried changing the exception to the proper one but it's not doing anything still. I'm thinking my try statement is incorrect. Here's what I have:

              try {
              if (Double.parseDouble(payment.getText()) < 0);
              }
              catch (NumberFormatException exception){
              System.out.println("Invalid payment amount.");
              }

              I've read the book's explanation of it probably 10 times now but I'm not getting it. Here's the example from the book. I'm having trouble trying to relate to this example and apply it to what I'm trying to do. I'm pretty sure my try statement is all wrong.

              try
              {
              String filename = . . .;
              FileReader reader = new FileReader(filename);
              Scanner in = new Scanner(reader);
              String input = in.next();
              int value = Integer.parseInt(input);
              . . .
              }
              catch (IOException exception)
              {
              exception.printStackTrace();
              }
              catch (NumberFormatException exception)
              {
              System.out.println("Input was not a number");
              }

              Comment


              • #97
                If payment.getText() is not a valid double (e.g. "foo") then that code will execute System.out.println("Invalid payment amount.").

                edit: this may be a dumb question, but you can see your Java console, right? When you call System.out.println() you can see the output.

                Code:
                if (Double.parseDouble(payment.getText()) < 0);
                Note: this code doesn't do anything because the text between the "if (condition)" and the next semicolon is empty.
                Last edited by Kuciwalker; October 20, 2010, 18:19.

                Comment


                • #98
                  Just so we're clear on something here, this code I'm trying to write is going to pop up a window saying "Invalid payment amount" not just print it in the console right?

                  Comment


                  • #99
                    Ultimately, yes, but I'm not actually familiar with swing. Just replace System.out.println with someMethodThatPopsUpAMessageBox.

                    Comment


                    • JOptionPane.showMessageDialog
                      "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                      Ben Kenobi: "That means I'm doing something right. "

                      Comment


                      • Thanks Asher. Ok and then I don't understand this statement
                        Note: this code doesn't do anything because the text between the "if (condition)" and the next semicolon is empty.
                        Thanks again for all the help so far. I wouldn't have figured these things out on my own I don't think.

                        Comment


                        • Code:
                          try {
                              if (Double.parseDouble(payment.getText()) < 0)
                                  /* see this empty bit here?  this is where the code that would be executed after the if(), if the condition were true, goes */;
                          }
                          catch (NumberFormatException exception) {
                              System.out.println("Invalid payment amount.");
                          }
                          Remember there are two forms of the if() syntax:

                          Code:
                          if ([i]condition[/i]) [i]statement[/i];
                          and

                          Code:
                          if ([i]condition[/i]) {
                              [i]a sequence of statements, each followed by a semicolon[/i]
                          }

                          Comment


                          • For example, the following program:

                            Code:
                            if (1 == 0)
                                System.out.println("foo");
                                System.out.println("bar");
                            will always output "bar".

                            But this program:

                            Code:
                            if (1 == 0) {
                                System.out.println("foo");
                                System.out.println("bar");
                            }
                            will print nothing.

                            Comment


                            • I guess what I don't understand then is how you make an if statement work within the try/catch block. If I have to have a statement following the if statement, which is actually the catch block, is an if statement not the thing to use?

                              Comment


                              • Think of the "try {" and "}" as demarking boundaries around otherwise perfectly normal code - they have nothing to do with your if statement.

                                If you just want to try calling Double.parseDouble() and see if it raises an exception, then all you need to do is say:

                                Code:
                                try {
                                    Double.parseDouble(someString);
                                } catch (NumberFormatException e) {
                                    //whatever code you want to execute if an exception is raised
                                }

                                Comment

                                Working...
                                X