Input validation loop issue - JAVA [SOLVED]
-
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\]); }
-
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\]); }
Please edit this and put
<pre>
tags around the code.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Please edit this and put
<pre>
tags around the code.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Edited, sorry, didn't know that the
was needed around the code. Thanks!
-
Edited, sorry, didn't know that the
was needed around the code. Thanks!
Well it still isn't set properly; take a look at the result of your edit. It should be: <pre lang="java"> // code here </pre> so it looks like
private boolean validateInput(String input1, String input2)
{
// rest of your Java code here
}Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
Well it still isn't set properly; take a look at the result of your edit. It should be: <pre lang="java"> // code here </pre> so it looks like
private boolean validateInput(String input1, String input2)
{
// rest of your Java code here
}Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
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!
-
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!
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 thanAND
? If the first value validates OK then you setcounter
to 1, but then break out of the while statement becauseinputValidArray[0]
is nowtrue
.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
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 thanAND
? If the first value validates OK then you setcounter
to 1, but then break out of the while statement becauseinputValidArray[0]
is nowtrue
.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
@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!
-
@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!
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
-
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
@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!
-
@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!
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
-
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
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!
-
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!
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
-
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
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.
-
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.
Yes, that's the way to go.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
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\]); }
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