Köll & Sam Classroom Blog

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    ps. also have a look of the JFormattedTextF ield; it takes care of most
    those nasty mechanics.

    kind regards,

    Jos

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      Originally posted by JosAH
      ps. also have a look of the JFormattedTextF ield; it takes care of most
      those nasty mechanics.

      kind regards,

      Jos
      Yes, I looked at it after I finished my little demo, but it allows you to enter characters outside of its preferred set and then rejects them at the end as opposed to my class which doesn't even allow you to enter non-numeric characters. However, JFormattedTextF ield would have been a better base class to use instead of JTextField. I'll leave that as an exercise for the reader. ;o)>>> Sam

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        btw, for a not-so-large range of integers I normally use a JSpinner; I like it
        to force the user into correct behaviour ;-)

        kind regards,

        Jos

        Comment

        • Dököll
          Recognized Expert Top Contributor
          • Nov 2006
          • 2379

          I think I may have something here guys, I've been putting it down and getting back into it to free my mind. Needs more work, I seem to be looping fine but I move on to next InputDialog even after data were found to be invalid. I do get the InputBox again to re-enter but only after I have gone through the others. I may have to go away and come back again, but what do you see happening:

          // Gets three input values--loan amount, interest rate, and
          // loan period

          [CODE=JAVA]
          private void getInput() {


          double loanAmount = 0, annualInterestR ate = 0;
          int loanPeriod = 0;
          String inputStr;


          //An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
          while (loanAmount < 100 || loanAmount > 1000000 ) {
          inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Amount (Dollars + Cents):");

          try {
          loanAmount = Double.parseDou ble(inputStr);
          // catch the user's entry here and throw an error
          //User will need to re-enter data to continue
          if (loanAmount < 100 || loanAmount > 1000000 ) {
          throw new Exception("Loan Amount is invalid, please try again...");
          }

          //Catch instances were user either adds in vowels or leaves pop up empty
          //Our user will need to re-enter information but the correct data type, digits only...
          } catch (NumberFormatEx ception e) {

          JOptionPane.sho wMessageDialog( null, "'" + inputStr
          + "' is an invalid data type\n"
          + "Please enter digits only");
          //Catch instances were user either adds in vowels or leaves pop up empty
          //Our user will need to re-enter information but the correct data type, digits only...
          } catch (Exception e) {

          JOptionPane.sho wMessageDialog( null, "Error: "
          + e.getMessage()) ;

          }

          while (annualInterest Rate < 5 || annualInterestR ate > 9 ) {
          inputStr = JOptionPane.sho wInputDialog(nu ll, "Annual InterestRate (9.5 %):");

          try {
          annualInterestR ate = Double.parseDou ble(inputStr);
          // catch the user's entry here and throw an error
          //User will need to re-enter data to continue
          if (annualInterest Rate < 5 || annualInterestR ate > 9 ) {
          throw new Exception("Loan Amount is invalid, please try again...");
          }

          //Catch instances were user either adds in vowels or leaves pop up empty
          //Our user will need to re-enter information but the correct data type, digits only...
          } catch (NumberFormatEx ception b) {

          JOptionPane.sho wMessageDialog( null, "'" + inputStr
          + "' is an invalid data type\n"
          + "Please enter digits only");
          //Catch instances were user either adds in vowels or leaves pop up empty
          //Our user will need to re-enter information but the correct data type, digits only...
          } catch (Exception b) {

          JOptionPane.sho wMessageDialog( null, "Error: "
          + b.getMessage()) ;


          }


          while (loanPeriod < 1 || loanPeriod > 30 ) {
          inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Period (Number of years):");

          try {
          loanPeriod = Integer.parseIn t(inputStr);
          // catch the user's entry here and throw an error
          //User will need to re-enter data to continue
          if (loanPeriod < 1 || loanPeriod > 30 ) {
          throw new Exception("Loan Period is invalid, please try again...");
          }

          //Catch instances were user either adds in vowels or leaves pop up empty
          //Our user will need to re-enter information but the correct data type, digits only...
          } catch (NumberFormatEx ception i) {

          JOptionPane.sho wMessageDialog( null, "'" + inputStr
          + "' is an invalid data type\n"
          + "Please enter digits only");
          //Catch instances were user either adds in vowels or leaves pop up empty
          //Our user will need to re-enter information but the correct data type, digits only...
          } catch (Exception i) {

          JOptionPane.sho wMessageDialog( null, "Error: "
          + i.getMessage()) ;


          }

          }

          }

          }

          [/CODE]

          I guess it is okay to loop through other InputDialog boxes to again arrive at the original InputDialog that errored, then re-enter the data. It'd be cool to get it to just go right back to the loanAmount Dialog after data were found to be incorrect, then move on to next Dialog, it's possible right!

          Almost forgot to mention, I am also trying to salvage ideas from your code to see if that remedies the issue...

          Thanks for posting:-)
          Last edited by Dököll; Mar 27 '08, 06:31 AM. Reason: text...

          Comment

          • Dököll
            Recognized Expert Top Contributor
            • Nov 2006
            • 2379

            Originally posted by Dököll
            I still cannot shake it guys, I am trying to have my InputDialog to loop back to me so I can re-enter, and only after that part is handled, errors checked should I move on to the next Input
            [CODE=JAVA]

            //An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
            while (loanAmount < 100 || loanAmount > 1000000 ) {
            inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Amount (Dollars + Cents):");

            try {
            loanAmount = Double.parseDou ble(inputStr);
            // catch the user's entry here and throw an error
            //User will need to re-enter data to continue
            if (loanAmount < 100 || loanAmount > 1000000 ) {
            throw new Exception("Loan Amount is invalid, please try again...");
            }

            //Catch instances were user either adds in vowels or leaves pop up empty
            //Our user will need to re-enter information but the correct data type, digits only...
            } catch (NumberFormatEx ception e) {

            JOptionPane.sho wMessageDialog( null, "'" + inputStr
            + "' is an invalid data type\n"
            + "Please enter digits only");
            //Catch instances were user either adds in vowels or leaves pop up empty
            //Our user will need to re-enter information but the correct data type, digits only...
            } catch (Exception e) {

            JOptionPane.sho wMessageDialog( null, "Error: "
            + e.getMessage()) ;

            }

            [/CODE]

            Looks like my only option thus far is go through all InputDialogs. Can anyone give me an idea how to finish checking loanAmount for valid data and if invalid to loop back to its InputDialog to re-enter stuff, pheew that was a long one:-)

            Thanks much!

            Dököll
            Last edited by Dököll; Apr 3 '08, 10:59 AM. Reason: uneeded text...

            Comment

            • Dököll
              Recognized Expert Top Contributor
              • Nov 2006
              • 2379

              Originally posted by SammyB
              http://java.sun.com/j2se/1.4.2/docs/...TextField.html[code=java]
              import java.awt.*;
              import javax.swing.*;
              public class NumericTest extends JApplet
              {
              public void init()
              {
              JPanel p = new JPanel();
              p.setLayout(new GridLayout(1,2) );
              JLabel lbl = new JLabel("Enter Number:");
              NumericField tb = new NumericField(1) ;
              p.setBackground (Color.LIGHT_GR AY);
              p.add(lbl);
              p.add(tb);
              this.getContent Pane().add(p);
              this.setSize(20 0, 40);
              }
              }
              [/code]
              Freaking fantastic, Sam!

              I can work with this. Please disregard my previous post, actually still throw your hat in, why not. Let's see what other option there are:-)

              But thanks for this piece of code.

              In a bit!

              Köll
              Last edited by Dököll; Apr 3 '08, 11:09 AM. Reason: text removed...

              Comment

              • SammyB
                Recognized Expert Contributor
                • Mar 2007
                • 807

                Originally posted by Dököll
                Freaking fantastic, Sam!
                Köll
                About time that you liked it! ;o)>>>

                Java class is all over for me. Squeeked by with a 106 average! LOL! Now, I'm taking an intro to XML. It's taught by the same teacher as the Java course, but it's all on-line and should be much less work.

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  Originally posted by SammyB
                  About time that you liked it! ;o)>>>

                  Java class is all over for me. Squeeked by with a 106 average! LOL! Now, I'm taking an intro to XML. It's taught by the same teacher as the Java course, but it's all on-line and should be much less work.
                  106 average! LOL! a definite WOW to me, good job... You can now relax... We have six more labs about and one final:-)

                  Thanks again!

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    Originally posted by SammyB
                    Now, I'm taking an intro to XML. It's taught by the same
                    teacher as the Java course, but it's all on-line and should be much less work.
                    Famous last words ...

                    kind regards,

                    Jos ;-)

                    Comment

                    • SammyB
                      Recognized Expert Contributor
                      • Mar 2007
                      • 807

                      Originally posted by JosAH
                      Famous last words ...

                      kind regards,

                      Jos ;-)
                      Just in case you didn't see it: If a programming language was a boat.. That Java cargo ship looks just like my final project!

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        Originally posted by SammyB
                        Just in case you didn't see it: If a programming language was a boat.. That Java cargo ship looks just like my final project!
                        If Java were a boat this would'be been the slick, smooth development experience.

                        kind regards,

                        Jos ;-)

                        Comment

                        • Dököll
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 2379

                          Originally posted by Dököll
                          Looks like my only option thus far is go through all InputDialogs. Can anyone give me an idea how to finish checking loanAmount for valid data and if invalid to loop back to its InputDialog to re-enter stuff, pheew that was a long one:-)

                          Thanks much!

                          Dököll
                          Please disregard this one; rahter than ending the loop at the end of the class, ending it as an item by itself allows the loop to continue and demands for legal data. I think I was trying to envelope all loops together, together this time did not work:

                          [CODE=JAVA]

                          //An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
                          while (loanAmount < 100 || loanAmount > 1000000 ) {
                          inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Amount (Dollars + Cents):");

                          try {
                          loanAmount = Double.parseDou ble(inputStr);
                          // catch the user's entry here and throw an error
                          //User will need to re-enter data to continue
                          if (loanAmount < 100 || loanAmount > 1000000 ) {
                          throw new Exception("Loan Amount is invalid, please try again...");
                          }

                          //Catch instances were user either adds in vowels or leaves pop up empty
                          //Our user will need to re-enter information but the correct data type, digits only...
                          } catch (NumberFormatEx ception e) {

                          JOptionPane.sho wMessageDialog( null, "'" + inputStr
                          + "' is an invalid data type\n"
                          + "Please enter digits only");
                          //Catch instances were user either adds in vowels or leaves pop up empty
                          //Our user will need to re-enter information but the correct data type, digits only...
                          } catch (Exception e) {

                          JOptionPane.sho wMessageDialog( null, "Error: "
                          + e.getMessage()) ;

                          }

                          }//This braket is therefore needed to end the loop for the loanAmount.
                          //Even though the InputDialog does in fact come it is annoying having to wait
                          //to see the box again. This one works better:-)

                          [/CODE]

                          In a bit, I have another brain tease; to my standards anyway:-)
                          Last edited by Dököll; Apr 6 '08, 11:36 PM. Reason: text...

                          Comment

                          • Dököll
                            Recognized Expert Top Contributor
                            • Nov 2006
                            • 2379

                            If I were to have the need to set my arrary size I would achieve it by doing this:

                            [CODE=JAVA]

                            Automobile [] automobile; //declare automobile array
                            automobile = new Automobile[5]; //create automobile array

                            [/CODE]

                            I have been toying with this bit of code in my book to allow a non-fixed array to become fixed and vise versa, just need some direction of how to go about it:

                            [CODE=JAVA]

                            int automobile; //declare automobile array
                            int [] number; //declare array size/number of automobiles here

                            number = Integer.parseIn t(
                            JOptionPane.sho wInputDialog(nu ll,"Enter as many automobiles needed, we suggest 3 at most:"));

                            number = new int[automobile];

                            ...

                            [/CODE]

                            I added the number right in here: [ ] , just to see, it did not like it there:-)

                            What are your thoughts?

                            Köll
                            Last edited by Dököll; Apr 7 '08, 12:04 AM. Reason: code...

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              Originally posted by Dököll
                              If I were to have the need to set my arrary size I would achieve it by doing this:

                              [CODE=JAVA]

                              Automobile [] automobile; //declare automobile array
                              automobile = new Automobile[5]; //create automobile array

                              [/CODE]

                              I have been toying with this bit of code in my book to allow a non-fixed array to become fixed and vise versa, just need some direction of how to go about it:

                              [CODE=JAVA]

                              int automobile; //declare automobile array
                              int [] number; //declare array size/number of automobiles here

                              number = Integer.parseIn t(
                              JOptionPane.sho wInputDialog(nu ll,"Enter as many automobiles needed, we suggest 3 at most:"));

                              number = new int[automobile];

                              ...

                              [/CODE]

                              I added the number right in here: [ ] , just to see, it did not like it there:-)

                              What are your thoughts?

                              Köll
                              My thoughts are that you don't really understand what objects and arrays are
                              all about. When you declare a non-primitive variable you actually have a remote
                              control to your television but you don't have a television yet. All TVs so to speak,
                              in Java, have to be 'new'd. That creates a TV for you and you can assign it to
                              your remote control (variable).

                              Also with arrays, which are objects. you have to 'new' them before you can
                              manipulate them with your remote control. Arrays always have a fixed size that
                              you set when you 'new' the array. There are no 'unfixed' arrays, they don't exist.

                              Java uses two different syntaxes for array remote controls:

                              [code=java]
                              int cStyle[];
                              int[] javaStyle;
                              [/code]

                              The first style is made to make those old C programmers feel more comfortable
                              but the second style is the prefered one. Both styles create a TV remote control
                              but there's no TV yet.

                              If you want to make an array larger or shorter you have to 'new' another array,
                              copy the corresponding elements from the old to the new array and set the
                              remote control to point to the new array. That's exactly what the ArrayList does.
                              There is no magic behind the scenes.

                              Arrays are a bit special objects w.r.t. their methods: arrays don't have them, i.e.
                              they're plain data objects and you even can't extend them by subclassing them
                              (there is nothing to subclass).

                              When you 'new' an array you can specify any number >= 0 because creating
                              arrays is done at runtime, not at compile time; that adds a bit more flexibility
                              than you have in C because in that language the size of the array must be
                              known to the compiler and therefore must be the value of a constant expression.

                              kind regards,

                              Jos

                              Comment

                              • BigDaddyLH
                                Recognized Expert Top Contributor
                                • Dec 2007
                                • 1216

                                Originally posted by Dököll
                                If I were to have the need to set my arrary size I would achieve it by doing this:
                                [CODE=JAVA]
                                Automobile [] automobile; //declare automobile array
                                automobile = new Automobile[5]; //create automobile array
                                [/CODE]
                                What JosAH said. Note how close you are from getting it right. Take your fixed size example and rewrite it as:
                                [CODE=JAVA]
                                Automobile [] automobile;
                                int automobileSize = ...; //compute size
                                automobile = new Automobile[automobileSize];
                                [/CODE]
                                or
                                [CODE=JAVA]
                                int automobileSize = ...; //compute size
                                Automobile [] automobile = new Automobile[automobileSize];
                                [/CODE]

                                Comment

                                Working...