473,883 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java Validation

I wish to validate a string but i'm not quite sure how to go about doing it.

I'd like to ensure that it is 9 characters long and in the following format.

"LL 00 LLL" - Thats letter, letter, space, number, number, space, letter,
letter, letter)

The letter sections mustn't contain numbers and is inputed from a string.

Any help greatly appreciated, including further reading from website.

Regards,
Dave
Jul 17 '05
18 21441
that's essentially what i proposed and will work great pending any
custom exception detections... (which would result in what i proposed)

learn something new everyday...

- perry

Murray wrote:

I think it's a matter of opinion which is more elegant really. For something
like this that's relatively simple, regexes don't really add much imho.

boolean match = yourString..mat ches("[a-zA-z]{2} [0-9]{2} [a-zA-z]{3}");
boolean match = yourString.matc hes("\\p{Alpha} {2} \\d{2} \\p{Alpha}{3}") ;


Jul 17 '05 #11
Liz
I think the rolled out version is much faster
than this.
And more clear.
And more maintainable.

"perry" <pe***@cplusplu s.org> wrote in message
news:Tz******** ************@ne ws20.bellglobal .com...
something like this...

it'll take you only five minutes and it'll be so much fun to do!

- perry

package samples;

/**
* <p>Title: SmartParse </p>
* <p>Descriptio n: Just an example validation class </p>
* <p>Copyright: Perry Anderson</p>
* @author Perry Anderson
* @version 1.0
*/

public class SmartParse {
String validation;

public SmartParse(Stri ng validation) {
this.validation = validation;
}

public class BadFormatExcept ion extends Exception {}

public void validate(String entry) throws BadFormatExcept ion {
if (entry == null || entry.length() != validation.leng th())
throw new BadFormatExcept ion();
for (int i = 0; i < entry.length(); i++) {
char c = validation.char At(i);
char e = entry.charAt(i) ;
if (Character.isLe tter(c) && !Character.isLe tter(e))
throw new BadFormatExcept ion();
else
if (Character.isDi git(c) && !Character.isDi git(e))
throw new BadFormatExcept ion();
else
if (Character.isSp aceChar(c) && !Character.isSp aceChar(e))
throw new BadFormatExcept ion();
}
}

public static void main(String[] args) {
SmartParse smartParse1 = new SmartParse("LLL 00 LLL");
try {
smartParse1.val idate("ABC 12 DEF");
}
catch (BadFormatExcep tion ex) {
System.out.prin tln("should not be thrown");
}
try {
smartParse1.val idate("123 AB 456");
}
catch (BadFormatExcep tion ex) {
System.out.prin tln("should be thrown");
}
}

}
Dave W wrote:
I wish to validate a string but i'm not quite sure how to go about doing it.
I'd like to ensure that it is 9 characters long and in the following format.
"LL 00 LLL" - Thats letter, letter, space, number, number, space, letter, letter, letter)

The letter sections mustn't contain numbers and is inputed from a string.
Any help greatly appreciated, including further reading from website.

Regards,
Dave

Jul 17 '05 #12
it's just a matter of what he wants to do...

if he's addressing one simple field with one simple expression check,
what you got there is as simple as it gets (especially for someone who
does not know regular expressions)

but the moment he's got to maintain more than one field with customized
error handling he's pretty much in a quadmire of "gotcha's" unless he
breaks down the problem into a series of resuable parts.....

just speaking from past experience

cheers

- perry
Liz wrote:
I think the rolled out version is much faster
than this.
And more clear.
And more maintainable.

"perry" <pe***@cplusplu s.org> wrote in message
news:Tz******** ************@ne ws20.bellglobal .com...
something like this...

it'll take you only five minutes and it'll be so much fun to do!

- perry

package samples;

/**
* <p>Title: SmartParse </p>
* <p>Descriptio n: Just an example validation class </p>
* <p>Copyright: Perry Anderson</p>
* @author Perry Anderson
* @version 1.0
*/

public class SmartParse {
String validation;

public SmartParse(Stri ng validation) {
this.validation = validation;
}

public class BadFormatExcept ion extends Exception {}

public void validate(String entry) throws BadFormatExcept ion {
if (entry == null || entry.length() != validation.leng th())
throw new BadFormatExcept ion();
for (int i = 0; i < entry.length(); i++) {
char c = validation.char At(i);
char e = entry.charAt(i) ;
if (Character.isLe tter(c) && !Character.isLe tter(e))
throw new BadFormatExcept ion();
else
if (Character.isDi git(c) && !Character.isDi git(e))
throw new BadFormatExcept ion();
else
if (Character.isSp aceChar(c) && !Character.isSp aceChar(e))
throw new BadFormatExcept ion();
}
}

public static void main(String[] args) {
SmartParse smartParse1 = new SmartParse("LLL 00 LLL");
try {
smartParse1.val idate("ABC 12 DEF");
}
catch (BadFormatExcep tion ex) {
System.out.prin tln("should not be thrown");
}
try {
smartParse1.val idate("123 AB 456");
}
catch (BadFormatExcep tion ex) {
System.out.prin tln("should be thrown");
}
}

}
Dave W wrote:
I wish to validate a string but i'm not quite sure how to go about doing
it.
I'd like to ensure that it is 9 characters long and in the following
format.
"LL 00 LLL" - Thats letter, letter, space, number, number, space,
letter,
letter, letter)

The letter sections mustn't contain numbers and is inputed from a
string.
Any help greatly appreciated, including further reading from website.

Regards,
Dave



Jul 17 '05 #13
Liz
I worked for a guy once and he came up with this instruction:
If you want to do something more than what is required
on this project, I will not pay even a dime for it. No matter
how much better it is, or how much more it will do; not a dime.
"perry" <pe***@cplusplu s.org> wrote in message
news:x0******** *************@n ews20.bellgloba l.com...
it's just a matter of what he wants to do...

if he's addressing one simple field with one simple expression check,
what you got there is as simple as it gets (especially for someone who
does not know regular expressions)

but the moment he's got to maintain more than one field with customized
error handling he's pretty much in a quadmire of "gotcha's" unless he
breaks down the problem into a series of resuable parts.....

just speaking from past experience

cheers

- perry
Liz wrote:
I think the rolled out version is much faster
than this.
And more clear.
And more maintainable.

"perry" <pe***@cplusplu s.org> wrote in message
news:Tz******** ************@ne ws20.bellglobal .com...
something like this...

it'll take you only five minutes and it'll be so much fun to do!

- perry

package samples;

/**
* <p>Title: SmartParse </p>
* <p>Descriptio n: Just an example validation class </p>
* <p>Copyright: Perry Anderson</p>
* @author Perry Anderson
* @version 1.0
*/

public class SmartParse {
String validation;

public SmartParse(Stri ng validation) {
this.validation = validation;
}

public class BadFormatExcept ion extends Exception {}

public void validate(String entry) throws BadFormatExcept ion {
if (entry == null || entry.length() != validation.leng th())
throw new BadFormatExcept ion();
for (int i = 0; i < entry.length(); i++) {
char c = validation.char At(i);
char e = entry.charAt(i) ;
if (Character.isLe tter(c) && !Character.isLe tter(e))
throw new BadFormatExcept ion();
else
if (Character.isDi git(c) && !Character.isDi git(e))
throw new BadFormatExcept ion();
else
if (Character.isSp aceChar(c) && !Character.isSp aceChar(e))
throw new BadFormatExcept ion();
}
}

public static void main(String[] args) {
SmartParse smartParse1 = new SmartParse("LLL 00 LLL");
try {
smartParse1.val idate("ABC 12 DEF");
}
catch (BadFormatExcep tion ex) {
System.out.prin tln("should not be thrown");
}
try {
smartParse1.val idate("123 AB 456");
}
catch (BadFormatExcep tion ex) {
System.out.prin tln("should be thrown");
}
}

}
Dave W wrote:

I wish to validate a string but i'm not quite sure how to go about
doing
it.
I'd like to ensure that it is 9 characters long and in the following


format.
"LL 00 LLL" - Thats letter, letter, space, number, number, space,


letter,
letter, letter)

The letter sections mustn't contain numbers and is inputed from a


string.
Any help greatly appreciated, including further reading from website.

Regards,
Dave


Jul 17 '05 #14
"Liz" <Li*@nospam.com > wrote in message news:<shYoc.672 3$6f5.547138@at tbi_s54>...
I worked for a guy once and he came up with this instruction:
If you want to do something more than what is required
on this project, I will not pay even a dime for it. No matter
how much better it is, or how much more it will do; not a dime.


There are two kinds of object orientation: tactical object orientation
and strategic object orientation.

Tactical OO is when you're given a set of requirements to implement
over a series of increments. These requirements can be thought of as
changes to a codebase, but changes over which you have (or your
Product Management have) total control.

If you spot two functions within these requirements that are merely
different implementations of the same interface, for example, then you
might code the interface and one implementation in increment 1, and
stick it behind a parameterised Factory pattern.

Then when you implement the second implementation in increment 2, you
know that clients will be able, with minimum impact (basically
changing the parameter in the call to the Factory), to use and test
this second implementation, because the interface is unchanged. This
is basic Variance Encapsulation, as described in GoF's Design
Patterns.

Strategic object orientation is trying to design variance
encapsulation for changes which will only come into play after your
product is released. You have no control over these changes. You can
sift nebulous standards and keep an eye on the newest technologies,
and you can guess how to design your code so that, should you be lucky
enough to have identified a future requirement/change, then your code
will be flexible enough allow the change without much re-work.

If you get strategic object orientation wrong, then you've wasted
loads of those dimes that your boss was concerned about.

That phrase, "... something more than what is required on this
project," sounds like tactical-thinking, and there's nothing wrong
with it. You could argue that strategic OOers risk more, and those
risks may ruin or win-all. If OO guaranteed results, no one would
write code in any other way. It doesn't, and they do.

Is your boss still in the business?

..ed

www.EdmundKirwan.com
Jul 17 '05 #15
Dave W wrote:
I wish to validate a string but i'm not quite sure how to go about doing it.

I'd like to ensure that it is 9 characters long and in the following format.

"LL 00 LLL" - Thats letter, letter, space, number, number, space, letter,
letter, letter)

The letter sections mustn't contain numbers and is inputed from a string.

Any help greatly appreciated, including further reading from website.


Try this :

public class Sample {
public static void main( String args[] )
{
String tests[] = { "LLL00LLL", "LLL00L0L", "000LL000" };

for ( int ctr = 0 ; ctr < tests.length ; ctr++ ) {
if ( tests[ctr].matches(
"\\p{Alpha}{3}\ \p{Digit}{2}\\p {Alpha}{3}" ) ) {
System.out.prin tln( tests[ctr] + " - match" );
} else {
System.out.prin tln( tests[ctr] + " - miss" );
}
}

return;
}
}

Tim

Jul 17 '05 #16
So much for checking my work....

The expression should be:

"\\p{Alpha} {2} \\p{Digit}{2} \\p{Alpha}{3}"

Tim

Tim Orbaker wrote:
Dave W wrote:
I wish to validate a string but i'm not quite sure how to go about
doing it.

I'd like to ensure that it is 9 characters long and in the following
format.

"LL 00 LLL" - Thats letter, letter, space, number, number, space, letter,
letter, letter)

The letter sections mustn't contain numbers and is inputed from a string.

Any help greatly appreciated, including further reading from website.

Try this :

public class Sample {
public static void main( String args[] )
{
String tests[] = { "LLL00LLL", "LLL00L0L", "000LL000" };

for ( int ctr = 0 ; ctr < tests.length ; ctr++ ) {
if ( tests[ctr].matches(
"\\p{Alpha}{3}\ \p{Digit}{2}\\p {Alpha}{3}" ) ) {
System.out.prin tln( tests[ctr] + " - match" );
} else {
System.out.prin tln( tests[ctr] + " - miss" );
}
}

return;
}
}

Tim


Jul 17 '05 #17
Liz

<ia********@hot mail.com> wrote in message
news:48******** *************** ***@posting.goo gle.com...
"Liz" <Li*@nospam.com > wrote in message news:<shYoc.672 3$6f5.547138@at tbi_s54>...
I worked for a guy once and he came up with this instruction:
If you want to do something more than what is required
on this project, I will not pay even a dime for it. No matter
how much better it is, or how much more it will do; not a dime.


There are two kinds of object orientation: tactical object orientation
and strategic object orientation.

Tactical OO is when you're given a set of requirements to implement
over a series of increments. These requirements can be thought of as
changes to a codebase, but changes over which you have (or your
Product Management have) total control.

If you spot two functions within these requirements that are merely
different implementations of the same interface, for example, then you
might code the interface and one implementation in increment 1, and
stick it behind a parameterised Factory pattern.

Then when you implement the second implementation in increment 2, you
know that clients will be able, with minimum impact (basically
changing the parameter in the call to the Factory), to use and test
this second implementation, because the interface is unchanged. This
is basic Variance Encapsulation, as described in GoF's Design
Patterns.

Strategic object orientation is trying to design variance
encapsulation for changes which will only come into play after your
product is released. You have no control over these changes. You can
sift nebulous standards and keep an eye on the newest technologies,
and you can guess how to design your code so that, should you be lucky
enough to have identified a future requirement/change, then your code
will be flexible enough allow the change without much re-work.

If you get strategic object orientation wrong, then you've wasted
loads of those dimes that your boss was concerned about.

That phrase, "... something more than what is required on this
project," sounds like tactical-thinking, and there's nothing wrong
with it. You could argue that strategic OOers risk more, and those
risks may ruin or win-all. If OO guaranteed results, no one would
write code in any other way. It doesn't, and they do.

Is your boss still in the business?


That was 20 years ago. It was in the new product development group,
and there was no assurance that the product would be accepted yet
alone be the base for enhanced features. Sometimes these sorts of
things would demonstrate that there is no demand or that a fundamental
restructuring would be necessary for viability.

.ed

www.EdmundKirwan.com

Jul 17 '05 #18
all things considered, i was in a position where just such "instructio n"
should have been applied.... instead, i had an over-zealous programmer
who wanted to write 10x to 100x the amount of code necessary yet
essentially halted all other resources and manpower for getting anything
done....

he was the stereo-typical "No" person and they are derived from the
passive-task oriented section of the grid, an area typical left to
"analysts". ...

the best way to deal with a "No" person is to highlight all the negative
aspects to your proposed solution before they get a chance to. that way
the "No", having the insane need to find something "wrong", invariably
cannot disagree with himself or performs a double negative... in
otherwords, he (or she) ends become a "resource" standing behind and
supporting your proposed solution.

childish as it may seem, the "No" person is able to sway the crowd
because he is able to broadcast that he simply wants to no one to make a
"terrible mistake". yet, in the real world, he ends up causing "No"
decision to be made at all, even at the total expense of the project...

and money is "No" object, that last project was a million dollars gone
into the ground... just before that another fanatical "No" person and
his insane "company wide standard" drowned a 66 million dollar project...

it's pretty funny, in certain situations, the failure of a given project
is what everybody wants.... after all, if you are being paid $165 per
hour just to be a "Yes" man why rock the boat...

getting back to what Liz was saying, there is something to be said about
knowing the requirements of a given project. in both of the above cases
that was my primary focus and usually is the best way to reach some sort
of target. the principle draw back to doing up the requirements however
is that any given "political" system is not necessarily going to be
interest in any form of "progress". ..

at the end of the day, all you can really do tread water....

check out Rick & Rick's seminar on "How to deal with Difficult People"
via the CareerTrack website. as sophisticaed as RUP and similiar
techniques can be, it all seems to be going back to the basic skill of
"learning" how to listen to people....

and Liz is speaking from experience...

cheers

- perry

Liz wrote:
I worked for a guy once and he came up with this instruction:
If you want to do something more than what is required
on this project, I will not pay even a dime for it. No matter
how much better it is, or how much more it will do; not a dime.


Jul 17 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4112
by: Brian | last post by:
I am having alot of trouble getting a XML document validated with a schema. I got a sample document and schema off of w3schools.com, which passed an online xml validator: http://tools.decisionsoft.com/schemaValidate.html. I cannot, however, get them validated programmatically. The documents are:
2
2156
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For example, using XPath or Java method. So is there a concept and/or standard for post-schema validation or application validation? Is there any hook in XML standards that allow me to hook up with my customized validation? Additionally and more...
2
7577
by: Marcin Cenkier | last post by:
Hi, I want to validate a DOM document, and if I build DOM from a stream using documentBuilder.parse() validation using validator.validate(DOMSource) works, but if I create the same document manually then validation throws an exception: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ....
0
2169
by: info | last post by:
Hi, Is it possible to include in the Schema validation file, every custom error message for each validation rules? This mean, in the same xsd file we can have the validation rules (patterns) and the custom error message throw during validation for the end user. I know .NET enable it using errorMessage tag, but with a standard W3C java validation ... If is it possible, does it works in IE too? Like that the same XSD file could be...
0
2783
by: pradheepayyanar | last post by:
JAVAians i have a small requirement which i am doin in java swing. i need to validate a field which will accept only numbers and dots. for example: i have a version number field which has to accept only in format like ***.***.***.*** but the numbers between the dots can vary accordingly. and the length can also vary note: or or or and so on... or
2
6984
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
1
4312
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in Linux there is an error. The code that fails is the following: SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); I´m sure that code is ok. In fact, I´ve found that in several...
0
1675
by: kokababu | last post by:
Hi, I have created an XML writer module to write xml using StaX api for our Rest Web Service(using Jersey). I am validating my XML against schema. It works Locally without any error but when I have deployed it in the production server, I have got the following error: java.lang.NullPointerException at com.sun.org.apache.xerces.internal.jaxp.validation.StAXValidatorHelper.validate(Unknown Source)
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7959
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5782
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3227
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.