Difference between revisions of "Input Validation"
m (Removing of <pre> tags from SolutionCode) |
(EDIT: Reworded the problem and added an input feedback message to the code) |
||
Line 1: | Line 1: | ||
{{1010PrAD|ProblemName=Input Validation | {{1010PrAD|ProblemName=Input Validation | ||
− | |Problem=Prompt the user | + | |Problem=Prompt the user to enter either a, b, or c. Validate the input and prompt the user to enter a choice until a valid input is entered. Display a message with the valid letter that the user entered. Use the following guidelines in coming up with a solution: |
* [[https://webmail.cs.umanitoba.ca/mediawiki/index.php/Input/Output_using_JOptionPane JOptionPane for input]] | * [[https://webmail.cs.umanitoba.ca/mediawiki/index.php/Input/Output_using_JOptionPane JOptionPane for input]] | ||
* [[While Loops|While loop]] for validating input | * [[While Loops|While loop]] for validating input | ||
Line 37: | Line 37: | ||
result = !(message.equals("a")) | result = !(message.equals("a")) | ||
</pre> | </pre> | ||
− | We can use this new knowledge to create the while loop that runs with invalid input. | + | We can use this new knowledge to create the while loop that runs with invalid input. |
<pre> | <pre> | ||
//loop until we get valid input | //loop until we get valid input | ||
Line 46: | Line 46: | ||
} | } | ||
</pre> | </pre> | ||
+ | Now we display a message to the user showing the valid letter that they entered. This message provides feedback to the user and lets them know that their entry was accepted. | ||
+ | <pre> | ||
+ | JOptionPane.showMessageDialog(null,"You entered \""+ message +"\"."); | ||
+ | </pre> | ||
+ | |||
Putting all of this together we can get the final solution which is under the code heading. | Putting all of this together we can get the final solution which is under the code heading. | ||
Line 66: | Line 71: | ||
message = JOptionPane.showInputDialog("Please Enter a, b or c:"); | message = JOptionPane.showInputDialog("Please Enter a, b or c:"); | ||
} | } | ||
+ | //display the user's validated input | ||
+ | JOptionPane.showMessageDialog(null,"You entered \""+ message +"\"."); | ||
} | } | ||
} | } | ||
}} | }} |
Revision as of 00:08, 9 April 2010
Back to the Program-A-Day homepage
ProblemPrompt the user to enter either a, b, or c. Validate the input and prompt the user to enter a choice until a valid input is entered. Display a message with the valid letter that the user entered. Use the following guidelines in coming up with a solution:
|
While and For Loops | |
---|---|---|
SolutionTo use JOptionPane you will need to include the following import statement. import javax.swing.JOptionPane; //needed for JOptionPane We will need one String variable message. For getting input from the JOptionPane input dialog. String message; //used to store the input from the user. To get input from the user we use the following line of code. //gets input from the user message = JOptionPane.showInputDialog("Please Enter a, b or c:"); To validate our input we are going to need to used the .equals() method. For strings this will compare the contents of two passed strings. The equals method returns true if the two strings matched. //checks if message is equal to a //returns true if true message.equals("a") For our problem we want to continue to ask until we get a valid letter. While loops keep running while the test condition = true. So we have to negate the returned value so that we get false to exit the loop on valid input. We can use "!" in front of the method call to negate the answer. boolean result; //result will now equal false when message = "a" result = !(message.equals("a")) We can use this new knowledge to create the while loop that runs with invalid input. //loop until we get valid input while(!message.equals("a") && !message.equals("b") && !message.equals("c")) { //gets input from the user message = JOptionPane.showInputDialog("Please Enter a, b or c:"); } Now we display a message to the user showing the valid letter that they entered. This message provides feedback to the user and lets them know that their entry was accepted. JOptionPane.showMessageDialog(null,"You entered \""+ message +"\".");
| ||
Code |