Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

storing strings in a specific format

 
Ben Tigger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I'm wondering if u could give me any ideas as to how i would go about storing a String in the following format:

Letter Letter space Number Number space Letter Letter Letter

its for storing car registration numbers

Cheers
[ May 27, 2004: Message edited by: Ben Tigger ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "storing"?

Are you rather asking about how to verify the format of a String? If that is the case, then a regular expression would certainly fit the bill well. If you'd like more information on how to use regular expressions in Java, just ask.
 
Ben Tigger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i mean is that the user enters the registration number and it is stored as a variable in that format. or is there any way to make sure that the user enters it in that format
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the user enter the value? Is it a text field on a form in a web page? Is it through a JOptionPane, or a frame-based application? Is it through the command prompt?

No matter the input mechanism, you can certainly format the input (which is harder depending on how many different user-input patterns you want to be able to handle), as well as verify that the input format is correct, and then prompt the user to input the value again if the format is wrong (which is much easier than the previous option).

To verify the pattern of the input data, you could make use of a few of the methods in the String class and the Character class to be able to analyze the overall length, as well as the type of each character individually.

A less tedious solution to verify the patter of the input data would be to make use of regular expressions. For what I hope is an easy introduction to that subject, take a look at the two articles I wrote for the JavaRanch Journal. (Search for "regex" on that page.)
[ May 27, 2004: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic