Results 1 to 6 of 6
Thread: Help in adding some validation..
- 11-28-2008, 02:07 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
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 04:16 PM.
- 11-28-2008, 04:45 PM #2
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
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.
- 11-28-2008, 08:38 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
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.
- 11-28-2008, 10:20 PM #4
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
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.
- 11-28-2008, 10:56 PM #5
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
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 02:49 AM.
- 11-30-2008, 01:36 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Need Help On Entry Validation
By obdi in forum New To JavaReplies: 3Last Post: 07-25-2008, 06:39 AM -
TextField Validation
By Mir in forum AWT / SwingReplies: 9Last Post: 06-29-2008, 06:28 AM -
form validation
By abhiN in forum New To JavaReplies: 0Last Post: 03-07-2008, 07:04 AM -
Swing Validation
By ppayal in forum AWT / SwingReplies: 0Last Post: 02-09-2008, 09:00 AM -
Swing validation
By ppayal in forum New To JavaReplies: 0Last Post: 02-09-2008, 08:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks