• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Comparison

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic