• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Comparison  RSS feed

 
SJ Carter
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am nearing completion on a program I am writing, but seem to have gone blank with regards to one last method.

I have got a string which contains 7 characters(a combination of letters and numbers). These characters should be stored either in the format 2 letters, 2 numbers, 3 letters or 1 letter, 3 numbers, 3 letters.

The method has to be able to determine whether the characters are stored in either of the above sequences, or it throws an exception. My problem is that I've managed to write code to compare the characters to the first format, but can't seem to get my head round being able to compare the string to both formats, and being able to distinguish between the two formats.

I have tried to use the StringTokenizer but as there is not obvious delimiter I don't know any other way of achieving this.

Any help will be greatly appreciated.

ps. I'm a newbie, so don't be too harsh with me

thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the java.util.regex package. If you are looking for a certain pattern, you can write a regular expression for each and match it against your string.

One quetion, when you say "there is not obvious delimiter" what do you mean by that? Does it mean that your three groups of characters can be delimited by absolutely anything, or that there is more than one delimiter allowed?
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use an if statement based on whether the second character is a letter or number to distinguish between the formats. Look at the Character class and the String charAt() method.



Glad I could help with your homework.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't forget to check for a null String or that the String length is long enough before trying to access any characters. Otherwise a NullPointerException or IndexOutOfBoundsExcpetion respectively, will be thrown.

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd vote for the regular expression solution over the 2nd character solution. The first job of code is to communicate to humans what you meant to do. Being explicit about the two formats is a good thing and would give you full validation to boot.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, I would as well. However, since they said this in their original post:

ps. I'm a newbie, so don't be too harsh with me


I gave them a solution that did not require that they learn too much beyond Java i.e. regular expressions.

6 one way , half a dozen another...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!