# Finding the Length of a Long Data Type

Page 1 of 1

## 3 Replies - 22531 Views - Last Post: 08 March 2012 - 12:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=269950&amp;s=fc98f77b98fafae209485e991b5233c7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 RedFox134

Reputation: 0
• Posts: 13
• Joined: 29-November 11

# Finding the Length of a Long Data Type

Posted 07 March 2012 - 10:52 PM

This is probably horribly inefficient, but it was the only way I could think to do it. I'm currently just trying to get the if statements to run properly.

Each if is supposed to be a different length of a credit card number. The first if is if the credit card number is 16 digits long, the second if is for 15 digits, the third if is for 14 digits, and the fourth if is for 13 digits. The last else is the error message.

Basically if I enter in 1234567890123456 it should run what ever I want it to do in the first if. If I enter in 13 I get the error message. The problem I'm having is that I get the error message when one of the if's should run and I get the first if when the error message should run. I've tried re-writing my parameters for each if statement and I can't seem to get it to run correctly. Am I doing this right and something else could be causing it or are my parameters off?

Thanks for the help!

```if((CC_Number <= ((999999999 * 10000000)+9999999) && (CC_Number >= (1000000000 * 1000000))))
{//begin of else if
System.out.println("\nFirst if");
}//end of if else
else if((CC_Number <= ((999999999 * 1000000)+999999) && (CC_Number >= (1000000000 * 100000))))
{//begin of 15 long credit card check
System.out.println("\nSecond if");
}//end of 15 long credit card check
else if((CC_Number <= ((999999999 * 100000)+99999) && (CC_Number >= (1000000000 * 10000))))
{//begin of 14 long credit card length
System.out.println("\nThird if");
}//end of 14 long credit card length
else if((CC_Number <= ((999999999 * 10000)+9999) && (CC_Number >= (1000000000 * 1000))))
{//begin of 13 long credit card length
System.out.println("\nFourth if");
}//end of 13 long credit card length
else
System.out.println("\nThat is not a valid credit card digit length."
+"\nA valid langth is between 13 and 16 characters long.");
```

In case it's difficult to read I put some spacing in between to make it more pleasing to the eye, or my eye at least.

```if((CC_Number <= ((999999999 * 10000000)+9999999) && (CC_Number >= (1000000000 * 1000000))))

{//begin of else if

System.out.println("\nFirst if");

}//end of if else

else if((CC_Number <= ((999999999 * 1000000)+999999) && (CC_Number >= (1000000000 * 100000))))

{//begin of 15 long credit card check

System.out.println("\nSecond if");

}//end of 15 long credit card check

else if((CC_Number <= ((999999999 * 100000)+99999) && (CC_Number >= (1000000000 * 10000))))

{//begin of 14 long credit card length

System.out.println("\nThird if");

}//end of 14 long credit card length

else if((CC_Number <= ((999999999 * 10000)+9999) && (CC_Number >= (1000000000 * 1000))))

{//begin of 13 long credit card length

System.out.println("\nFourth if");

}//end of 13 long credit card length
else

System.out.println("\nThat is not a valid credit card digit length."
+"\nA valid langth is between 13 and 16 characters long.");
```

Is This A Good Question/Topic? 0

## Replies To: Finding the Length of a Long Data Type

### #2 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

## Re: Finding the Length of a Long Data Type

Posted 07 March 2012 - 11:07 PM

It would be a whole lot easier if you did just convert the long to a String, and then got the length of it. Also it would be easier for people to read your code this way, because you are not doing any calculations.

```String numberAsString = CC_Number.toString();

if (numberAsString.length >= 16) {
}
else if (numberAsString >= 15) {
}
else if (numberAsString >= 14) {
}
else if (numberAsString >= 13) {
}
else {
}

```

Edit: The above code doesn't take the '-' symbol into account

This post has been edited by CasiOo: 07 March 2012 - 11:20 PM

### #3 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

## Re: Finding the Length of a Long Data Type

Posted 07 March 2012 - 11:24 PM

This code piece is taken from the java 1.7 source code. Just in case you wanted to know how they are finding the size of the int

```    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };

// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}

```

the method is then called by
```int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);

```

### #4 RedFox134

Reputation: 0
• Posts: 13
• Joined: 29-November 11

## Re: Finding the Length of a Long Data Type

Posted 08 March 2012 - 12:46 AM

Thanks for the help CasiOo! Converting it into a String seems a lot easier and cleaner to do. I'll give that a shot and see if it fixes my if problem. Thanks for the help!