• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fixed-length numbers and strings  RSS feed

 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have to make a critical decision about API usage. I would be grateful if some of you could share their experiences. This will help me make an informed decision and avoid potential trouble further down the line.

I have to use fixed-length integer numbers and fixed-length strings such as for example:

-A string that is exactly 5-letter long (no longer and no shorter than 5 letters). e.g. "abcde"
-A number that is exactly 8-digit long (no longer and no shorter than 8 digits). e.g. "12345678"

Ideally I would get some sort of exception when the "container" for the letters or digits contains less or more than what the spec says.

I am not sure which classes or primitives could meet my needs. As far as the fixed-length string is concerned I initially thought of using a array of chars but an array of chars is not that easy to manipulate and contains "garbage" until you explicitely fill it in with data. I'd rather some sort of class.

Can anyone please advise me?

Thanks in advance,

Julien.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Julien Martin:
I am not sure which classes or primitives could meet my needs. As far as the fixed-length string is concerned I initially thought of using a array of chars but an array of chars is not that easy to manipulate and contains "garbage" until you explicitely fill it in with data. I'd rather some sort of class.

An array in Java does not contain random "garbage" when you don't explicitly fill it with data. If you create a char array, the elements are initialized to zero (the char with code \u0000).

Why not just use String, and in your API check the length of the String at the appropriate places, and possibly throw an IllegalArgumentException (or any other exception you like) when the String doesn't contain 5 characters or 8 digits?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your two choices, as I see them, are

1) Just use String and int, and have duplicated code all over the place that checks your conditions; or

2) Define classes named FiveCharacterString and EightDigitInteger, and put the check code in their constructors. Giving them the obvious "toString()" and "intValue()" methods would let you work with these objects as easily as with the primitives.

Option #2 is probably better, although of course it depends on the exact application. For example, if memory is tight and there are very many of these values, the objects add unnecessary overhead.

By the way: I'd actually give these classes better names from the application domain -- i.e., PartNumber or PatientID or whatever they really are.

By the wayt
 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
I have a clearer idea of what I am going to do as far as Strings are concerned.
I am still don't know how to proceed in order to have a fixed-length integer.
Any idea what class representing an int could go into my number wrapper?
Julien.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not an int ?
Your validation would then be
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd just store it as an int. The constructor should check that it's an eight-digit int and throw an exception if not; then the class should be immutable (provide no way to change the value, and make the instance variable final).
 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
One last question: what would be the best way to count the number of digits in an int without converting it to an string??
Julien.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at Joanne's post above -- that's the answer.
 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much to all of you!!!
[ October 05, 2007: Message edited by: Julien Martin ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!