Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Input validation loop issue - JAVA [SOLVED]

Input validation loop issue - JAVA [SOLVED]

Scheduled Pinned Locked Moved Java
helpjavatutorialquestion
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L LEKnowlton

    Oh, I didn't have the code highlighted first and it showed up at the top of my code. So, I copied the and put it at the end of my code (but forgot to delete it from the top). :doh: Should be all set now for review!

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #6

    See how much easier that is to read? The statement at the top of your loop is:

        while (!inputValidArray\[0\] || !inputValidArray\[1\])
    

    Are you sure that should be an OR expression rather than AND? If the first value validates OK then you set counter to 1, but then break out of the while statement because inputValidArray[0] is now true.

    Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

    L 1 Reply Last reply
    0
    • L Lost User

      See how much easier that is to read? The statement at the top of your loop is:

          while (!inputValidArray\[0\] || !inputValidArray\[1\])
      

      Are you sure that should be an OR expression rather than AND? If the first value validates OK then you set counter to 1, but then break out of the while statement because inputValidArray[0] is now true.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      L Offline
      L Offline
      LEKnowlton
      wrote on last edited by
      #7

      @Richard Yes, I agree that using the formatting codes makes the code much more readable! As far as my code goes: Ah I understand what your saying about using the OR instead of AND, but I don't think I'm having a problem with that because each inputValidArray is negated using the exclamation. So, the loop will not be broken unless both are not false. I could have used: while (!(inputValidArray[0] && inputValidArray[1])) instead. I think my main issue is that I can't get the code to stop and allow the user to fix the input in jTextField1 after it determines the input is bad. I set the focus and highlight it, but the code continues on and looks at jTextField2 and does what it should. Then it comes back up and checks input one again, which is already 'bad' and basically creates a loop I have to manually break out of. Does that make sense? Thanks!

      L 1 Reply Last reply
      0
      • L LEKnowlton

        @Richard Yes, I agree that using the formatting codes makes the code much more readable! As far as my code goes: Ah I understand what your saying about using the OR instead of AND, but I don't think I'm having a problem with that because each inputValidArray is negated using the exclamation. So, the loop will not be broken unless both are not false. I could have used: while (!(inputValidArray[0] && inputValidArray[1])) instead. I think my main issue is that I can't get the code to stop and allow the user to fix the input in jTextField1 after it determines the input is bad. I set the focus and highlight it, but the code continues on and looks at jTextField2 and does what it should. Then it comes back up and checks input one again, which is already 'bad' and basically creates a loop I have to manually break out of. Does that make sense? Thanks!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #8

        I misread the code and your question earlier. I think what you need to do is add some break statements to get out of the loop when one of the values is bad. This should then allow the user to correct the input and then you can call the validation method once again. Something like:

        Create_Error_Message_Dialog_Box(errorMessage);

        textFieldArray[counter].requestFocusInWindow();

        break; // break out of the while() loop

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        L 1 Reply Last reply
        0
        • L Lost User

          I misread the code and your question earlier. I think what you need to do is add some break statements to get out of the loop when one of the values is bad. This should then allow the user to correct the input and then you can call the validation method once again. Something like:

          Create_Error_Message_Dialog_Box(errorMessage);

          textFieldArray[counter].requestFocusInWindow();

          break; // break out of the while() loop

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          L Offline
          L Offline
          LEKnowlton
          wrote on last edited by
          #9

          @Richard You know, I thought of that and that would be the easiest way out. I was hoping there was a way to keep the loop going, but, I guess I was in denial! lol ;) Thanks for your help!

          L 1 Reply Last reply
          0
          • L LEKnowlton

            @Richard You know, I thought of that and that would be the easiest way out. I was hoping there was a way to keep the loop going, but, I guess I was in denial! lol ;) Thanks for your help!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #10

            I'm not sure (obviously) how the whole app fits together, but this would be the 'normal' way of doing things.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            L 1 Reply Last reply
            0
            • L Lost User

              I'm not sure (obviously) how the whole app fits together, but this would be the 'normal' way of doing things.

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              L Offline
              L Offline
              LEKnowlton
              wrote on last edited by
              #11

              I totally agree! I let myself get hung up on not breaking the loop. Bad input should break the loop, since proper input is mandatory. I've pretty much just started learning java on my own, but had taken a C# course last semester, so I have a little bit of a leg up on it. For a linux class project, I'm going to make an IPV4 subnet calculator using the linux Netbeans IDE. So, I thought I'd make a simple GUI program in Netbeans to figure out the GUI mechanics. The program this code is from is a drastically simple calculator that adds two floats (if valid) and displays the result in a third textbox. Now, its just a matter of figuring out how to get mnemonics and keybinds to work for the Add, Clear and Exit buttons. The Netbeans IDE isn't all that clear on how to do that, so the digging for information continues :) Thanks for your time on this rather simple fix!

              L 1 Reply Last reply
              0
              • L LEKnowlton

                I totally agree! I let myself get hung up on not breaking the loop. Bad input should break the loop, since proper input is mandatory. I've pretty much just started learning java on my own, but had taken a C# course last semester, so I have a little bit of a leg up on it. For a linux class project, I'm going to make an IPV4 subnet calculator using the linux Netbeans IDE. So, I thought I'd make a simple GUI program in Netbeans to figure out the GUI mechanics. The program this code is from is a drastically simple calculator that adds two floats (if valid) and displays the result in a third textbox. Now, its just a matter of figuring out how to get mnemonics and keybinds to work for the Add, Clear and Exit buttons. The Netbeans IDE isn't all that clear on how to do that, so the digging for information continues :) Thanks for your time on this rather simple fix!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #12

                LEKnowlton wrote:

                The Netbeans IDE isn't all that clear

                Sometimes the only way to this information is to use the tutorials[^] and the API documentation[^].

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                L 1 Reply Last reply
                0
                • L Lost User

                  LEKnowlton wrote:

                  The Netbeans IDE isn't all that clear

                  Sometimes the only way to this information is to use the tutorials[^] and the API documentation[^].

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  L Offline
                  L Offline
                  LEKnowlton
                  wrote on last edited by
                  #13

                  From what I've gleaned from searches on this: I'm thinking that instead of strictly using the mouse click event for each button that I can use the actionevent listener and set up a case logic structure to choose which action does what.

                  L 1 Reply Last reply
                  0
                  • L LEKnowlton

                    From what I've gleaned from searches on this: I'm thinking that instead of strictly using the mouse click event for each button that I can use the actionevent listener and set up a case logic structure to choose which action does what.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #14

                    Yes, that's the way to go.

                    Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                    1 Reply Last reply
                    0
                    • L LEKnowlton

                      Hello All, I'm trying to validate two input text boxes and am running into a snag. I'm using a while loop that runs while either one of the inputs is 'false'. The problem is that it doesn't stop and allow for a user to correct the first input if its bad and goes on to check the next input. I could do this before when I just checked one input at time and called the validate method twice. Trying to do both in one method is proving to be a pain. Any ideas? Here's my validate method:

                      private boolean validateInput(String input1, String input2)
                      {

                          Boolean\[\] inputValidArray = {false , false};
                          String\[\] inputArray = {input1, input2};
                      
                          JTextField\[\] textFieldArray = {jTextField1, jTextField2};
                      
                          float numberToValidate = 0;
                          int counter = 0;   
                          String errorMessage = "";
                      
                          while (!inputValidArray\[0\] || !inputValidArray\[1\])
                          {
                              try
                              {
                                  // Check for empty or null input
                                  if (!(inputArray\[counter\].isEmpty() || inputArray\[counter\] == null))
                                  {
                                      numberToValidate = Float.valueOf(inputArray\[counter\]);
                                      inputValidArray\[counter\] = true;                
                                  }
                                  else
                                  {
                                      errorMessage = "This field can not be left blank!\\n" +
                                                     "  Please enter a valid number!\\n" +
                                                     "  (example: 123.45 or -123.45)";
                      
                                      Create\_Error\_Message\_Dialog\_Box(errorMessage); 
                      
                                      textFieldArray\[counter\].requestFocusInWindow();
                                
                                  }
                      
                              }
                              catch (NumberFormatException nfe)
                              {
                                  errorMessage = inputArray\[counter\] + " is not a valid number!\\n" +
                                                 "Please enter a valid number!\\n" +
                                                 "(example: 123.45 or -123.45)";
                      
                                  Create\_Error\_Message\_Dialog\_Box(errorMessage);
                      
                                  textFieldArray\[counter\].requestFocusInWindow();
                                  textFieldArray\[counter\].selectAll();
                      
                              }
                      
                              counter += 1;
                      
                          }
                          
                          return (inputValidArray\[0\] && inputValidArray\[1\]);
                      }
                      
                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #15

                      Someone down voted this question for no reason that I can see, so I added a +5 in compensation.

                      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups