# Thread: Help comparing the first digit of an integer

1. Member
Join Date
Mar 2015
Posts
23
Rep Power
0

## Help comparing the first digit of an integer

Hey guys, so I'm making a program that tracks packages and defines them based on their weight, type of casing, etc... This is all described in my tracking number. So first, I'm asked to create an abstract class for the Package type :

Java Code:
```package assignment2;

public abstract class Package {
protected long trackingNumber;
private double weight,cost;

public Package(long number, double weight, double cost)
{
trackingNumber = number;
this.weight = weight;
this.cost = cost;
}
abstract void tracking();
}```
Then I created 4 different child class. Letter ( Which will use the firstDigit method to see whether or not the letter is correctly weighted, etc... So let's say the number is 012345, then it would be accepted as a letter weighted in ounce, otherwise the tracking number will be change), then woodcrate which will do the same but check whether or not it is a wooden crate, etc... so on and so forth. Anyway, I was creating the Letter class and it seemed weird to me. I have this:

Java Code:
```package assignment2;

public class Letter extends Package {
public Letter(long trackingNumber, double weight, double cost)
{
super(trackingNumber,weight,cost);
}
@Override
public void tracking() {

}

}```
However, in the tracking method, I'd have to create it so it only checks the first digit of my number. So I thought of putting it to a string and checking whether or not index 0 is a 0 or 1. However, that seems very inefficient and I don't know if some of you would have an idea on how to create something more.... useful?

Thank you!! -Marc

2. ## Re: Help comparing the first digit of an integer

putting it to a string and checking whether or not index 0 is a 0 or 1.
Integer numbers in computers are stored in binary using the 8 bits of bytes. The Integer class has methods to convert the binary value of an int to a String with specified radix/base. The common base for most people is 10.
Numbers when converted to Strings are not given leading 0s. If you want the value of 0123 to be different from 123 you will need to save the values as Strings, not as ints.

3. Member
Join Date
Mar 2015
Posts
23
Rep Power
0

## Re: Help comparing the first digit of an integer

Well, the thing is, wouldn't that create a danger where someone would screw up and create a tracking number that isn't only numbers but would create,let's say, a tracking number that is alphanumerical. So let's say I use the attribute
Java Code:
` private String trackingNumber`
in order to have a tracking number with either a leading 0 or 1. However, someone could easily tamper with it and create a tracking number like this: 1a2334. no? I mean I could create conditions withing my mutator but it seems long for no reasons. Unless I'm missing something here :/

4. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
28

## Re: Help comparing the first digit of an integer

Validate the tracking number.

If there is a rule behind what values are allowed in a model then the model should be validating them and throwing an error.

5. ## Re: Help comparing the first digit of an integer

Even though "trackingnumber" is called a number, it's not a number: it's an identifier. Just like a telephonenumber is not a number but an identifier. You don't caluculate with them. They are Strings. As Tolls said: make it a String, and make sure that your setTrackingNumber(String) method pukes when it's not valid according to your rules.

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
14

## Re: Help comparing the first digit of an integer

Originally Posted by SurfMan
As Tolls said: make it a String
Poor Norm :(

7. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
28

## Re: Help comparing the first digit of an integer

I'm actually Norm in disguise...that's why I don't have an avatar, otherwise it would be too obvious.

8. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
14

## Re: Help comparing the first digit of an integer

Originally Posted by Tolls
I'm actually Norm in disguise...
Poor Norm :(

9. ## Re: Help comparing the first digit of an integer

I swear the forum software swapped the names! I had it right! I blame PHP!

10. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
14

## Re: Help comparing the first digit of an integer

Originally Posted by SurfMan
I swear the forum software swapped the names! I had it right! I blame PHP!
Actually, I would believe that.

11. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
28

## Re: Help comparing the first digit of an integer

Originally Posted by gimbal2
Poor Norm :(
I would argue about that, but it's a fair cop, frankly.

12. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: Help comparing the first digit of an integer

Originally Posted by Manddd
However, someone could easily tamper with it and create a tracking number like this: 1a2334. no? I mean I could create conditions withing my mutator but it seems long for no reasons. Unless I'm missing something here :/
Forget about conditions. Make an immutable tracking number class that validates the tracking number when it is constructed. Once it is validated, then break it apart and assign various fields or values to internal state variables. Then use getters to get the part of the tracking number you are interested in. In some cases, the getters could be boolean tests (like isFragile, isSpecialDelivery). It depends on how the tracking number is encoded. But it should only need to be validated once.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•