Difference between revisions of "Input Validation"

From CompSciWiki
Jump to: navigation, search
 
(22 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Input Validation
 
{{1010PrAD|ProblemName=Input Validation
  
|Problem=Input Validation
+
|Problem=Prompt the user to enter either a, b, or c and once valid input is entered display a message with the valid input. Validate the users input by prompting for input until the desired input is received.  Use the following guidelines when coming up with a solution:
 +
* Input using [[JOptionPane Methods|JOptionPane]]
 +
* [[While Loops|While loop]] for validating input
  
 +
|SideSectionTitle=While and For Loops
  
 
|SideSection=
 
|SideSection=
[[Image:OperatingSystemExample.jpg|float|267px]]
+
[[Image:Wiki_loops03.jpg|center]]<BR>
<BR>
+
Taken from http://www.flickr.com/photos/daniello/565304023/
+
  
An image or By Students section
+
|Solution=
 +
To use JOptionPane you will need to include the following import statement.
 +
<pre>
 +
import javax.swing.JOptionPane; //needed for JOptionPane
 +
</pre>
 +
We will need one String variable message. For getting input from the JOptionPane input dialog.
 +
<pre>
 +
String message; //used to store the input from the user.
 +
</pre>
 +
To get input from the user we use the following line of code.
 +
<pre>
 +
message = JOptionPane.showInputDialog("Please Enter a, b or c:"); //gets input from the user
 +
</pre>
 +
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.
 +
<pre>
 +
//checks if message is equal to a
 +
//returns true if true
 +
message.equals("a")
 +
</pre>
 +
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.
 +
<pre>
 +
boolean result;
  
|Solution=The solution...
+
result = !(message.equals("a")) //result will now equal false when message = "a"
 +
</pre>
 +
We can use this new knowledge to create the while loop that runs with invalid input.
 +
<pre>
 +
//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:");
 +
}
 +
</pre>
  
|SolutionCode=
+
An alternate (but equally valid) loop condition would be
 
<pre>
 
<pre>
import javax.swing.JOptionPane;
+
while(!(message.equals("a") || message.equals("b") || message.equals("c")))
 +
</pre>
 +
It is not necessarily obvious why these two are the same, but they are.
 +
 
 +
Once the loop is exited, we know that the user has typed an acceptable input ("a","b" or "c"). We can now 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.
 +
 
 +
|SolutionCode=import javax.swing.JOptionPane; //needed for JOptionPane input dialog
  
 
public class InputValidation
 
public class InputValidation
 
{
 
{
    public static void main(String [ ] args)
+
  public static void main(String [ ] args)
    {
+
  {
        String message;
+
      String message; //used to store the input from the user.
  
        message = JOptionPane.showInputDialog("Please Enter a, b or c:");
+
      //gets input from the user
 +
      message = JOptionPane.showInputDialog("Please Enter a, b or c:");
  
        while()
+
      //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:");
 +
      }
 +
      //display the user's validated input
 +
      JOptionPane.showMessageDialog(null,"You entered \""+ message +"\".");
 +
  }
 
}
 
}
</pre>
 
 
 
}}
 
}}

Latest revision as of 11:26, 1 December 2011

Back to the Program-A-Day homepage

Problem

Prompt the user to enter either a, b, or c and once valid input is entered display a message with the valid input. Validate the users input by prompting for input until the desired input is received. Use the following guidelines when coming up with a solution:

 

While and For Loops

Wiki loops03.jpg

Solution

To 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.

message = JOptionPane.showInputDialog("Please Enter a, b or c:"); //gets input from the user

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 = !(message.equals("a")) //result will now equal false when message = "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:");
}

An alternate (but equally valid) loop condition would be

while(!(message.equals("a") || message.equals("b") || message.equals("c")))

It is not necessarily obvious why these two are the same, but they are.

Once the loop is exited, we know that the user has typed an acceptable input ("a","b" or "c"). We can now 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 +"\".");


Putting all of this together we can get the final solution which is under the code heading.

Code

Solution Code

Back to the Program-A-Day homepage