obsessiveCookie
obsessiveCookie

Reputation: 1138

Phone Number String to int

Say I have the following variables:

String start = "02071231234"; 
String end = "02071231237"; 
List<String> numbersFromStartToEnd = new ArrayList<String>();

What's the best way to store: "02071231234", "02071231235", "02071231236", "02071231237" in numbersFromStartToEnd.

I tried changing the String to int and hoped to use a loop to create a list of Strings:

int startInt = Integer.parseInt(start);

but I'm getting

java.lang.NumberFormatException: For input string: "02885449730"

I guess it's because the number has a leading zero.

Upvotes: 1

Views: 322

Answers (3)

Eric Olson
Eric Olson

Reputation: 2985

I am not sure that any response is an actual solution. I created a small bit of code which gives the result (["02071231234", "02071231235", "02071231236", "02071231237"]) you are looking for:

public static void main(String[] args) {
    String start = "02071231234";
    String end = "02071231237";
    String leadingZeros = "";
    List<String> numbersFromStartToEnd = new ArrayList<String>();

    // get leading zeros (makes the assumption that all numbers will have same qty of leading zeros)
    for(char digit : start.toCharArray()) {
        if(digit != '0') { break; }
        leadingZeros += "0";
    }

    // set up your BigInts
    BigInteger s = new BigInteger(start);
    BigInteger e = new BigInteger(end);
    BigInteger one = new BigInteger("1");

    // collect all numbers from start to end (adding on any leading zeros)
    while (s.compareTo(e) <= 0) {
        numbersFromStartToEnd.add(leadingZeros + s.toString());
        s = s.add(one);
    }

    // print the result
    for(String string: numbersFromStartToEnd) {
        System.out.println(string);
    }
}

Upvotes: 0

jlordo
jlordo

Reputation: 37843

The problem is not the leading zero, see:

int x = Integer.parseInt("0123");
System.out.println(x); // prints 123

The problem is that your number is bigger than Integer.MAX_VALUE.

If I were you, I'd store the phone number either as a string, or a custom PhoneNumber class with fields like country code, area code, number and so on.


Update: Here's how you can check if one number is between two other numbers, provided that your strings only consist of digits:

    String start   = "02071231234"; 
    String end     = "02071231237";
    String toCheck = "02071231235";
    if (start.compareTo(toCheck) < 0 && end.compareTo(toCheck) > 0) {
        System.out.println("toCheck is between start and end");
    } else {
        System.out.println("toCheck is NOT between start and end");
    }

Upvotes: 3

Java Devil
Java Devil

Reputation: 10969

Problem is your number is larger than max possible integer value

Integer.MAX_VALUE = 2147483647

Use a BigInteger like this

BigInteger myBigInt = new BigInteger("my String");

Then you can still just do myBigInt + 1 to get the next

or if you do trim the leading zero you should be able to use Integer as long as your number does not go above Integer.MAX_VALUE

Upvotes: 0

Related Questions