3 Replies Latest reply: Oct 11, 2007 11:12 AM by 796280 RSS

    handling negative numbers.....

    807600
      ok so i forgot how to do this handling.....i have this....
                          
      Side1 = JOptionPane.showInputDialog("What is the size of the first side? NOTE If you wish to exit, enter a -1");
      sides1 = Integer.parseInt(Side1);
      how can i force the code back up to the option pane so they can re enter a positive integer value if they entered a negative one in the first place?

      basically if the user enters -8 6 times i wanna throw him a message every time saying that is not correct and then have the program be re-directed to the input box. thanks for your time guys!

      Mike
        • 1. Re: handling negative numbers.....
          807600
          1. Why not use dialogs in the usual manner and let the user exit by pressing the cancel button? In that case, method showInputDialog returns null, right?

          2. To repeat something, like repeatedly displaying a dialog, most people use a loop:
          int reply=0;
          String message = INITIAL_MESSAGE;
          while (reply <= 0) {
              show dialog with message
              if user pressed cancel, break from loop
              else if user entered a non-positive value set message = ANNOYED_SECOND_MESSAGE
              else reply will be a positive number and you're on your way out of this loop
          }
          I would make that code a method. You'll probably use it more than once, to gather different inputs, not?
          • 2. Re: handling negative numbers.....
            796280
            DrakosatOU wrote:
            ok so i forgot how to do this handling.....i have this....
                                
            Side1 = JOptionPane.showInputDialog("What is the size of the first side? NOTE If you wish to exit, enter a -1");
            sides1 = Integer.parseInt(Side1);
            how can i force the code back up to the option pane so they can re enter a positive integer value if they entered a negative one in the first place?

            basically if the user enters -8 6 times i wanna throw him a message every time saying that is not correct and then have the program be re-directed to the input box. thanks for your time guys!

            Mike
            Sorry if I'm getting a little off track from the original focus of the question....

            What if you simply limited the user's input options to keep them from entering a negative number in the first place? A JFormattedTextField and a MaskFormatter would do the trick. I love MaskFormatter because you can customize your valid character set. Forgive the sloppiness, but try something like this:
                 protected MaskFormatter myFormatter(String s) {
                     MaskFormatter formatter = null;
                     try {
                         formatter = new MaskFormatter(s);
                         formatter.setValidCharacters("<whatever>");
                     } 
                     catch (java.text.ParseException exc) { 
                          
                     }
                     return formatter;
                 }
                 
                 private JFormattedTextField myTextField = new JFormattedTextField(myFormatter("**"));
            // Where ** is limited to two characters, *** is limited to 3, etc.
            In setValidCharacters, you can limit it to only numbers (which is actually a default in the MaskFormatter) but I put that example up there to show you that you can do anything with it, really.... For example, if you want all hex characters, !, ?, and <space> to be valid characters, do this:
            formatter.setValidCharacters("0123456789abcdefABCDEF!? ");
            Some documentation here: http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html

            Hope that helps.

            Edited by: Thok on Oct 11, 2007 9:12 AM
            • 3. Re: handling negative numbers.....
              807600
              i see what your saying with a method use, i was thinking that myself, basicly my program take in 3 postive integer values and sends them to a triangle class the triangle class decides based on the input what type of triangle is Equalaterial, Scalene, or Isoclese (pardon the spelling) a method would be great but it would only excute once right when its called. ill try your method though, DrLaszloJamf.

              Mike

              Edited by: DrakosatOU on Oct 11, 2007 9:17 AM

              Edited by: DrakosatOU on Oct 11, 2007 9:18 AM