tags:

views:

271

answers:

6

Hi all, sorry for being such a noob first of all.

I'm having trouble storing a mobile number in one of my applications.

Was wondering if there is a Integer class that will allow you to store such a number starting with (0417254482) or would using a string be a more appropriate data type? At present when i try to use such a number with ints, double & lang data types it display some random number and not the one i passed.

Thanks.

+13  A: 

I would suggest using String - aside from anything else, otherwise you won't be able to store leading zeroes. You definitely shouldn't use int (too small) float or double (too much risk of data loss); long or BigInteger could be appropriate (aside from the leading zeroes problem), but frankly I'd go with String. That way you can also store whatever dashes or spaces the user has entered to make it easier to remember the number, if you want to.

Jon Skeet
Thanks for the fast response.
+1 for leading zeroes
Thorbjørn Ravn Andersen
+2  A: 

Every number have infinity amount of zeros on the left and right side,

To represent it you should use a string formating

class PhoneNumber implements Comparable<PhoneNumber> {

    private Long number;

    public PhoneNumber(Long number) {
        this.number = number;
    }

    public Long getNumber() {
        return this.number;
    }

    public boolean equals(Object object) {

        if (getNumber() == null && object == null) {
            return true; //or false its depend 
        }

        return getNumber().equals(object);
    }

    public int compareTo(PhoneNumber that) {

            if(that == null) {
             return -1;
            }

        Long thisNumber = getNumber();
            Long thatNumber = that.getNumber();

        if (thisNumber == null && thatNumber == null) {
            return 0; //or -1
        }

        if (thisNumber == null && thatNumber != null) {
            return -1;
        }

        return thisNumber.compareTo(thatNumber);

    }

    @Override
    public String toString() {
        return String.format("%010d", getNumber());
    }
}

Used %010d mean %[argument_index$][flags][width][.precision]conversion

flag 0 - padding zeros 10 - amount of padding zeros d - decimal integer

The implementation of interface Comparable give you the posibility to sort List.

List<PhoneNumber> phoneNumbers = new ArrayList();
 phoneNumbers.add(new PhoneNumber (123L);
 phoneNumbers.add(new PhoneNumber (123777L);
 phoneNumbers.add(new PhoneNumber (125L);
 phoneNumbers.add(new PhoneNumber (124L);
 phoneNumbers.add(new PhoneNumber (126L);

Collections.sort(phoneNumbers);

  for(PhoneNumber phoneNumber : phoneNumbers) {
   System.Console.Out.WriteLine(phoneNumber);
  }

The output is

 0000000000 
 0000000123
 0000000124
 0000000125
 0000000126
 0000123777

Comparable String Formatter

Vash
The code has a few mistakes (missing semi colons) but apart from that it's the perfect way to solve it.
matto1990
@matto1990 Only one ;-) but corrected all. thx
Vash
+1  A: 

Create your own PhoneNumber class with a private field of type String to represent it.

public class PhoneNumber {
   private String number;
   public PhoneNumber(String number) {
      //check validity of number
      this.number = number;
   }
   //getter, comparator, etc...
}

You could also represnt the number with long or BigInteger if all phone numbers have the same length, but be careful with leading zeros.

A phone number is not really an integer (or a string). It is something else which shuld have a class of its own.

EDIT: one more thing: I wouldn't implement a setter for this class because a phone number object would better be immutable

snakile
Adding a compareTo and equals method would make the sorting work as the questioner asked in this case.
matto1990
@matto: Your'e right. He should implement compareTo and equals. Thanks.
snakile
if you're just storing the number itself in an atomic String there's no need to wrap that into the new class since all the methods like hashCode, ... would essentially delegate to the implementations provided by String.
Johannes Wachter
A: 

You should use string to support numbers with leading zeros. The code you provided was:

Order order1 = new PickUpOrder(orderTime, 0473519954); 
//The pickup order requires an orderTime (String) and a contact number(Int). Heres    
//the constructor for PickUpOrder. 

public PickUpOrder(Date orderTime, String number) 
{ 
    discount = .2; 
    phoneNumber = number; 
    super.setOrderTime(orderTime); 
    //Test print 
    System.out.println(phoneNumber) 
    //reads int as 74049273 instead of 0473519954 
}

In the constructor, the number is string but when you call the constructor you used an int for phone number. There must have been a compile error here in java I think. Is this the same code you compiled?

Raze2dust
+7  A: 

Think about this: Is a phone number really a number? Does it make sense adding (or make another arithmetic operation) with phone numbers? Phone numbers are codes, they're usually represented with numbers, but that's just a convention and, maybe, in another country the use letters too (I've just realized, what about international phone numbers? they have a + at the beginning. You have to think about the nature of the things you want to represent, and then, find the most suitable representation.

manolowar
+1 I guess you can replace + by 00 so Denmark (where I am) is 0045 XXXX XXXX.
lasseespeholt
Yes. That is true + can be used instead of 00 and vice versa.
Vash
A: 

Although phone numbers are named numbers, they are normally not numbers (e.g. leading zeros, country prefix +XX, ...).

So there are two possibilities to represent a phone number correctly inside a program:

  1. Using String to keep the whole number like entered.
  2. Using a custom data type that offers additional support for phone number features

    public class PhoneNumber implements Comparable<PhoneNumber>{
    
    
    
    private String countryCode;
    
    
    private String areaCode;
    
    
    private String subscriberNumber;
    
    
    // Constructor(s)
    
    
    // Getter
    
    
    // HashCode + Equals
    
    
    // compareTo
    
    
    @Override
    public String toString(){
        return countrycode + " " + areaCode + " " + subscriberNumber;
    }
    

    }

It's really interesting to look at all the different conventions that are used internationally

Johannes Wachter
Additionally it also depends on how complex your applications gets. A normal application would only need String representation and nothing more whereas complex applications for keeping addresses and performing queries onto them would need more sophisticated handling.
Johannes Wachter