how to store mobile number in java? - java

I am trying to store the 10 digit mobile number in int, but it is saying that its out of range, how can i store in int, any help pls

There is no semantic value in storing it as a number - you won't be doing any numerical operations on it. You should store the value as a string.

A mobile number might start with a 0 (zero) therefore I wouldn't advice to store it as a number. My advice would be to store it as a String. Otherwise you should use a long.

What about a proper TelephoneNumber type class:
public interface TelephoneNumber
A simple re-usable entity class that defines attributes of a telephone number.

You'd better use String, but you can also use long or BigInteger.
But using String will allow you to store dashes or spaces between numbers.

Mobile numbers uses and E.164 numbering format for telephone or digital communications. You will have to use Strings as valid characters such as "+"/"-" can be found in mobile numbers.

Use a String, for for proper semantics I would wrap it in your own MobileNumber class.

Did you have a look at Java's BigInteger? Two possible references are:
Oracle BigInteger
Java Notes BigInteger

use Long or String instead of Integer. Or give us some code examples for what you are trying to do.

Related

How display currency with Freemarker when using Longs

So let's say I have:
class Person
{
private long salary;
}
Where 5345273 would be equal to $53,452.73 (in other words the last two digits are the cents.
Is there a way to directly reference ${Person.salary} so that it displays the proper amount?
I did see that there are pre-defined formats such as Currency but there doesn't appear to be one if you use longs. I did see it was possible to create your own custom formatter but is this the only way? And if so is there anyone who already created one because I can't imagine I'm the only person using long to manage currency amounts. And also is that the correct solution?
Should be possible to convert the long to a double in the expression. Something like this: ${(Person.salary/100)?string.currency}

Converting string to integer, but preceding zero is being removed -Follow up

This question has been answered so please close it...
Thanks for the clarifications!!
I looked at the question above but there is an use case which we should consider before closing the issue:
I have a situation where I raise an order and the system generates a reference number as: 0000002443
I store that number as a string.
When the system sends the order out, it sends two documents. One as a requisition with the above reference number and the other as a Purchase order with a reference: 0000002444
I need to be able to store the first reference number (i.e. 0000002443) as an Integer keeping the preceding zeroes and add +1 and store as a PO reference number (i.e.0000002444) to verify the orders later.
If I keep the first reference number as a String then I won't be able to add 1 to the reference number to get the PO reference Number.
It's a Follow up question:
https://stackoverflow.com/questions/15025136/converting-string-to-integer-but-preceding-zero-is-being-removed
Integers do not have leading zeros (as it says in that other question)
You'd need to convert it to an int, add one, and then pad it back into a String:
def ref = '0000002443'
def refPlusOne = "${ref.toInteger() + 1}".padLeft( ref.length(), '0' )
Simply put, an integer doesn't have a number of leading zeroes. It doesn't even have information about whether it's decimal, hex, or anything like that. It's just an integer.
If you really need to follow your existing design, I suggest you parse it as an integer, add one, and then repad with as many zeroes as you need to get back to the original length.
To be honest, if it's really just meant to be a number, it would be better if you stored it as a number instead of using a string at all.

Comparing phone numbers in Android

I need to compare two phone numbers to determine if they're from the same sender/receiver. The user may send a message to a contact, and that contact may reply.
The reply usually comes in
+[country-code][area-code-if-any][and-then-the-actual-number] format. For example,
+94 71 2593276 for a Sri Lankan phone number.
And when the user sends a message, he will usually enter in the format (for the above example) 0712593276 (assume he's also in Sri Lanka).
So what I need is, I need to check if these two numbers are the same. This app will be used internationally. So I can't just replace the first 2 digits with a 0 (then it will be a problem for countries like the US). Is there any way to do this in Java or Android-specifically?
Thanks.
Android has nice PhoneNumberUtils, and i guess your looking for :
public static boolean compare (String a, String b)
look in :
http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
using it should look like this :
String phone1
String phone2
if (PhoneNumberUtils.compare(phone1, phone2)) {
//they are the same do whatever you want!
}
android.telephony.PhoneNumberUtils class provides almost all necessary functions to deal with phone numbers and standards.
For your case, the solution is PhoneNumberUtils.compare(Context context, String number1, String number2) or else PhoneNumberUtils.compare(String number1, String number2).The former one checks a resource to determine whether to use a strict or loose comparison algorithm, thus the better choice in most cases.
PhoneNumberUtils.compare("0712593276", "+94712593276") // always true
PhoneNumberUtils.compare("0712593276", "+44712593276") // always true
// true or false depends on the context
PhoneNumberUtils.compare(context, "0712593276", "+94712593276")
Take a look at the official documentation. And the source code.
How about checking if the number is a substring of the receiver's number?
For instance, let's say my Brazilian number is 888-777-666 and yours is 111-222-333.
To call you, from here, I need to dial additional numbers to make international calls. Let's say I need to add 9999 + your_number, resulting in 9999111222333.
If RawNumber.substring(your_number) returns true I can say that I'm calling you.
just apply your logic to remove () and -
and follow PhoneNumberUtils

Most elegant isNumeric() solution for java

I'm porting a small snippet of PHP code to java right now, and I was relying on the function is_numeric($x) to determine if $x is a number or not. There doesn't seem to be an equivalent function in java, and I'm not satisfied with the current solutions I've found so far.
I'm leaning toward the regular expression solution found here: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric
Which method should I use and why?
Note that the PHP isNumeric() function will correctly determine that hex and scientific notation are numbers, which the regex approach you link to will not.
One option, especially if you are already using Apache Commons libraries, is to use NumberUtils.isNumber(), from Commons-Lang. It will handle the same cases that the PHP function will handle.
Have you looked into using StringUtils library?
There's a isNumeric() function which might be what you're looking for.
(Note that "" would be evaluated to true)
It's usually a bad idea to have a number in a String. If you want to use this number then parse it and use it as a numeric. You shouldn't need to "check" if it's a numeric, either you want to use it as a numeric or not.
If you need to convert it, then you can use every parser from Integer.parseInt(String) to BigDecimal(String)
If you just need to check that the content can be seen as a numeric then you can get away with regular expressions.
And don't use the parseInt if your string can contain a float.
Optionally you can use a regular expression as well.
if (theString.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")))
return true;
return false;
Did you try Integer.parseInt()? (I'm not sure of the method name, but the Integer class has a method that creates an Integer object from strings). Or if you need to handle non-integer numbers, similar methods are available for Double objects as well. If these fail, an exception is thrown.
If you need to parse very large numbers (larger than int/double), and don't need the exact value, then a simple regex based method might be sufficient.
In a strongly typed language, a generic isNumeric(String num) method is not very useful. 13214384348934918434441 is numeric, but won't fit in most types. Many of those where is does fit won't return the same value.
As Colin has noted, carrying numbers in Strings withing the application is not recommended. The isNumberic function should only be applicable for input data on interface methods. These should have a more precise definition than isNumeric. Others have provided various solutions. Regular expressions can be used to test a number of conditions at once, including String length.
Just use
if((x instanceof Number)
//if checking for parsable number also
|| (x instanceof String && x.matches("((-|\+)?[0-9]+(\.[0-9]+)?)+"))
){
...
}
//---All numeric types including BigDecimal extend Number

base-n series generator for a given number in java,,

I want to create a program for generating the series for the given base-n. ,
for example if my input is 2,then series shuould be, 00,01,10,11,etc.,(binary)
if my input is 10,then series shuould be,1,2,3,4,5,etc.,(decimal)
is there any general mechanism to find these numbers so that I can program for base-n.,
UPDATE:-
After,working out.,i face issue.
If I want to process that integer how to do that? Some body commented that, BaseInteger class I should use. please elaborate
You could use Integer's toString(int i, int radix) method for that.
For example:
Integer.toString(2, 2) // number 2, base 2
returns the string:
"10"
Note that the radix should be between 1 and 36.
You might be looking for something like this (take a peek at "Algorithm: Constructing Base b
Expansions"):
https://docs.google.com/viewer?url=http://websupport1.citytech.cuny.edu/faculty/dkahrobaei/Integers%2520and%2520Algorithms.pdf
I think you should first figure in which format you need the results. If they should be Strings, Bart's answer would probably suit you. An integer representation, which does actually mean something else (e.g. the int 10 does mean 2 with base 2) seems awkward to me. If i would need something like you described, i would probably implement a BaseNumber class first.

Categories