Results 1 to 6 of 6
  1. #1
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default Help in adding some validation..

    Hi,

    Can I ensure that data is only accepted inside a inputDialog if it is an integer, and if not display an error message?
    Because at the moment I get errors in the general output screen if somebody doesn't put an integer inside, because it tries to parse string into an integer.

    So I need some validation, so that it only parses the string into an integer if it is an integer! else it should display an error. If you understand !
    Last edited by tornado; 11-28-2008 at 05:16 PM.

  2. #2
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    What are you using to get your input? The Scanner class has a method called hasNextInt that you can use to validate what the user entered.

  3. #3
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    To get the value i'm using:

    userValue= JOptionPane.showInputDialog etc..
    Then parse userValue

    but I need a way to say if user value does not = an integer then show another dialog (error message).
    I know how to show the other dialog, it's just making the if statement to check if uservalue is an int before parsing it, because if it tries to parse something other than an integer it can result in error.

  4. #4
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    You can take care of this in a number of ways. The first is to use a try/catch to catch an exception if it occurs and print out your error message.

    I would imagine that this is not a great practice as it relies on exception handling to actually complete programming logic.

    The second option that comes to mind is to use the Pattern/Matcher classes along with a good regular expression that will validate the string you are going to parse first.

    So you create a Pattern that represents any integer value, then make a Matcher out of it and see if your string matches that Pattern. If it does, parse your int, if not print error message.

  5. #5
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    I made an example program using the Pattern/Matcher classes, I intentionally left out the correct regular expression. You will have to research that further.
    Java Code:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
    
    	/**
    	 * @author Adam Duke
    	 * @param args
    	 */
    	public static void main( String[] args ) {
    
    		// create the Pattern object
    		// INSERT THE APPROPRIATE REGULAR EXPRESSION IN THE DOUBLE QUOTES IN THE
    		// NEXT LINE
    		Pattern testPattern = Pattern.compile( "" );
    
    		// create a numeric String object
    		String integerString = "133813";
    
    		// create a non-numeric String object
    		String nonIntegerString = "blahblahblah";
    
    		// create Matcher objects for the test strings
    		Matcher integerStringMatcher = testPattern.matcher( integerString );
    		Matcher nonIntegerStringMatcher = testPattern.matcher( nonIntegerString );
    
    		// check the matchers and print results
    		if ( integerStringMatcher.matches() ) {
    			System.out.println( "The string '" + integerString + "' is an integer" );
    		}
    		else {
    			System.out.println( "The string '" + integerString + "' is not an integer" );
    		}
    		if ( nonIntegerStringMatcher.matches() ) {
    			System.out.println( "The string '" + nonIntegerString + "' is an integer" );
    		}
    		else {
    			System.out.println( "The string '" + nonIntegerString + "' is not an integer" );
    		}
    	}
    }
    Last edited by racerxadam; 11-30-2008 at 03:49 AM.

  6. #6
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Thanks Adam, it's really appreicated i'll attempt this later for sure.

Similar Threads

  1. Need Help On Entry Validation
    By obdi in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 07:39 AM
  2. TextField Validation
    By Mir in forum AWT / Swing
    Replies: 9
    Last Post: 06-29-2008, 07:28 AM
  3. form validation
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 03-07-2008, 08:04 AM
  4. Swing Validation
    By ppayal in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2008, 10:00 AM
  5. Swing validation
    By ppayal in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 09:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •