Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-14-2007, 03:39 PM
Member
 
Join Date: Aug 2007
Posts: 8
lockmac is on a distinguished road
java validation?
Hi.. i need to validate two different Strings.

The first String has to only be characters and no numbers
The second String has to be only numbers and no characters. How would I go about doing this?

Many thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-14-2007, 03:49 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
I would recommend you use a couple of regex functions to test this. That would probably be the easiest way of doing it:
Code:
String numbers = "0123456789"; String letters = "abcde"; String rubbish="$%^&£\""; Pattern numberPattern = Pattern.compile("^[0-9]+$"); Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$"); System.out.print("Numbers should match number pattern: "); System.out.println(numberPattern.matcher(numbers).matches()); System.out.print("Numbers should not match letter pattern: "); System.out.println(letterPattern.matcher(numbers).matches()); System.out.print("Letters should match letter pattern: "); System.out.println(letterPattern.matcher(letters).matches()); System.out.print("Letters should not match number pattern: "); System.out.println(numberPattern.matcher(letters).matches()); System.out.print("Rubbish should not match number pattern: "); System.out.println(numberPattern.matcher(rubbish).matches()); System.out.print("Rubbish should not match letter pattern: "); System.out.println(letterPattern.matcher(rubbish).matches());
I would recommend making the Patterns private static final in your class so they are only compiled once this will make it pretty efficient.

Hope that helps,
Shane.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-14-2007, 04:26 PM
Member
 
Join Date: Aug 2007
Posts: 8
lockmac is on a distinguished road
Thanks for the reply... im not too sure though how to use your code..

im learning java at the moment and not too advanced.. learning interface class at the moment.

thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-14-2007, 05:34 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
OK I'll put it into context a bit more then:
Code:
import java.util.regex.Pattern; /** * Validates String objects. * * @author Shane Preater */ public class StringValidator { private static final Pattern numberPattern = Pattern.compile("^[0-9]+$"); private static final Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$"); /** * Tests the given string contains only numbers. * * @param string * the string to test * @return <code>true</code> is it only contains numbers. */ public static boolean isAllNumbers(String string) { return numberPattern.matcher(string).matches(); } /** * Tests the given string contains only letters. * * @param string * the string to test * @return <code>true</code> is it only contains letters. */ public static boolean isAllLetters(String string) { return letterPattern.matcher(string).matches(); } }
This will allow you to simply test your strings against the methods then in your main method you can do:
Code:
public class Main { /** * @param args */ public static void main(String[] args) { String numberString = "123"; StringValidator.isAllNumbers(numberString); String letterString = "abc"; StringValidator.isAllLetters(letterString); } }
Obviously you will need to edit the code in the Main class to get the strings from where ever you are expecting them and then call the methods as per the example.

If you need more help give me a shout.
Shane.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Live Email Validation In Java shameel Web Frameworks 7 10-08-2008 01:40 PM
Struts validation Jack Web Frameworks 3 10-06-2008 01:54 AM
Swing Validation ppayal AWT / Swing 0 02-09-2008 10:00 AM
Swing validation ppayal New To Java 0 02-09-2008 09:59 AM
javascript validation yuchuang New To Java 5 05-14-2007 04:38 PM


All times are GMT +3. The time now is 03:09 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org