Here is an example of what I'm trying to do:
If a phone number is entered as 6123044356 it will display as (612)304-4356. Should a guest hit the backspace key 5 times instead of removing the last 4 numbers entered and the dash, remove the last 5 numbers entered. The phone number at that point would be displayed as (612)30. Should the guest hit the backspace key 3 more times the last 3 numbers displayed would be removed, not the parenthesis. The phone number would then be displayed as 61.
Any suggestions on how to do this?
Look at libphonenumber library. It has AsYouTypeFormatter class in it, which will handle this for you.
Alternatively you can use PhoneNumberFormattingTextWatcher which basically does the same thing. You would use it like so:
editText.addTextChangeListener(new PhoneNumberFormattingTextWatcher());
I would do it just like the dialer does it. That way is most intuitive.
Basically when they are typing, as soon as a number, say 1234567, gets to 7 digits, split it up like 123-4567 for the user. Then when it reaches 10 digits, 123-4567890, split it up like (123)456-7890 for them. When they reach 11 digits, change the format to 1-(234)567-8901.
When they are removing numbers from an 11 digit number, as soon as they hit 10 digits, change back to (123)456-7890. After that, as soon as the number of digits is less than 10, change the format back to 123-456789. Keep it in that format until they reach six digits when you just change it to 123456.
By changing the numbers to and from recognizable forms when a user reaches a certain number of digits, it subtly alerts the user that they have either created a real number, or there are too many or not enough digits for this to be a real number.
I suggest using the following thresholds:
11 digits: 1-(234)567-8901
10 digits: (123)456-7890 (anything less than this looks like 7 digits with digits on the end)
7 digits: 123-4567 (anything less just removes the dash).
I hope that makes sense. I do not think the user should have to input/erase any extra characters like parentheses and dashes. It should all be taken care of by the app.
Related
In my filter used into EditText, I want to be sure the user can only set .5 or .0 for decimal values.
Valid values examples:
34.5
34.0
34
Invalid values examples:
34.2
34.8
34.6
I tried this one, but it doesn't work properly: [0-9]*[.]?[0|5]
Thank you very much guys!
You're probably looking for [0-9]*(\.([50][0]*)*)*.
[0-9]*: Any character from 0 to 9, zero or more times [so that just a "." (= 0.0) input is valid]
\.: You need to escape the '.' character, since it usually would mean "any character", and you especifically need the dot there.
[50][0]*: First, either five or zero (once). Second, the 0 character, zero or more times (since 35.50 = 35.5). This also avoids inputs like 35.59 from being valid, since 9 != 0.
([50][0]*)*: This occurrence zero or more times, so that 35., for instance, becomes a valid input (since 35. = 35.0).
(\.([50][0]*)*)*: As for this grouping, it's in order to check for the five or the zero only if there is a decimal dot. It's grouping the dot character and the 5/0 logic together with a star (zero or more times) at the end, so if it doesn't occur, it still matches.
Let me know if this was what you were looking for.
To verify the whole numbers in the examples, you can make the last part optional and use anchors.
^[0-9]+(?:[.][05])?$
^ Start of string
[0-9]+ Match 1+ digits 0-9
(?:[.][05])? Optionally match . and a digit 0 or 5
$ End of string
See a regex demo.
If you want to be able to only type a pattern like that and also accept empty strings or 34. you can repeat the digit 0 or more times, optionally match . and optionally match either 0 or 5.
^[0-9]*[.]?[05]?$
See another regex demo
What may be the regex for allowing 17 digits with or without spaces in-between any number of the characters?
I am using a OCR real-time capture SDK (ABBYY RTR SDK) on Android(, and I'm using a custom data capture (which has to capture 17 digits with or without spaces in-between anywhere)
My regex is currently this:
field.setRegEx( "[0-9]{14,16}" );
This is only able to capture in-line digits (38492039483726473)
But I need it to capture digits in that format, as well as this format - 3849 20394 8372 6473.
How do I go about achieving this?
If it's simply those two formats:
(\d{17}|\d{4}\s\d{5}\s(?:\d{4}\s){2})
Captures a digit 17 times
or
Capture 4 digits followed by a space, then 5 digits followed by another space, and then 4 digits followed by a space twice
Try repeating a digit with optional space:
(\d ?){17}
Demo
An alternative to the answer given by #mrzasa would be to first remove all whitespace from the number string, and then use a simpler regex to check for 17 digits:
String input = "3849 20394 8372 6473";
if (input.replaceAll("\\s+", "").matches("\\d{17}")) {
System.out.println("match");
}
We know that there are numbers with different length. In Europe we mostly have 9 digits numbers plus country code.
In North America we often find 10 digits numbers.
I am trying to get my head around an idea how to get a country code from a number that may be of different length.
Any ideas? Maybe you know some working libs that can do it?
The key facts:
The country code is always at the start of the number, so it is easy to find no matter the length of the number.
There is no overlap, as #Luis points out.
A (looks pretty) complete list of country codes is give here. If you sort them by length (shortest first) and run through the list comparing the first n digits with the list entries you will get the answer.
However, if you look at the list you wall see that there are various groups of codes. A more intelligent approach would note that:
All numbers beginning with 1 are US, Canada or other US related places in which case the next three digits tell you which.
7 is Khazakstan
Apart from 20, all country codes beginning with 2 are three digits.
and so on ...
Country codes are parsed left-to-right with deterministic endpoints similar to the idea of Huffman coding. ie, if you see a 1 first, stop, it's the US/Canada/related territories. If you see most other numbers besides 7 (Russia/Kazakhstan), keep going. Some of those numbers may terminate on the second value.
The list of country codes is here: http://www.howtocallabroad.com/codes.html
It should be trivial for you to take this and write your own string parser of a phone number in order to determine which country code is present.
(don't forget that if these are numbers from within a particular country, you also have to take that country's exit code into account, which is also on the page I linked)
Edit: Oh, I guess luis covered it. But Jakob is incorrect in his comment about Barbados. Barbados is the same country code as the US; the 246 is its local "area code" within the US/Canada's country code.
I assume that you are talking about phone number country codes. Country codes are defined by the ITU ( https://en.wikipedia.org/wiki/List_of_country_calling_codes ). The country codes can be 1, 2 or 3 digits. Your only alternative is to have a list of all country codes and parse it from there. Note that there is no overlap; for instance, +44 belongs to the UK, and no country starts with just 4.
UPDATE: The North American Area has 4 digit prefixes, not 1, composed of +1 and a NPA of 3 digit (http://en.wikipedia.org/wiki/North_American_Numbering_Plan). The same rule applies though, in that +1-NPA cannot be repeated. Barbados seems to be +1246, but no other country or region can start with +1246. You can get the list of all NPA from http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes
I am working on a project in which i am getting value from excel sheet(in assets android) and reflecting data in list view.
problem is:: phone no is not in proper format.
9.777123455E9 instead of 9777123455
When it's a phone number, you should always store the cell data as text, even if it consists only of digits, since a phone number is no mathematical number and when doing operations on it, you want to treat it as a string of characters (i.e. text).
If you input a phone number that looks to Excel like a mathematical number, it will interpret it as a number and in consequence will do things to it that make sense for numbers, but not necessarily for phone numbers, such as displaying it in scientific format.
To force Excel to treat your number as text, precede it with a single quote (apostrophe) when entering it. That is, enter into the cell:
'9777123455
It will be displayed without the single quote, just as you expect a phone number to be displayed and can be processed as text.
double d=9.777123455E9;
NumberFormat formatter = new DecimalFormat("#");
System.out.println(d);
System.out.println(formatter.format(d));
output
9.777123455E9
9777123455
E9 simply means multiply by 10^9
Update:
As #blubberdiblub mentioned, for phone numbers, it makes sense to change it to text. But for other cases, If you need to do mathematical operations leaving it in the scientific format works. You can right click on the column name and select formatting option to set the type of data the column will handle (number , text etc). If you want don't want to change the phone number to text and still see the number, simply increase the width of the column. The number will be shown full (without the "E").
I am now working on a calculator, and everything works fine except for decimal places.
The calculator contains 2 displays actually, one is called fakedisplay for actual operations, and one is called Display, for presenting the desired format, ie adding commas.
When pressing 12345.678, Display will follow fakedisplay and present as 12,345.678, but if i press 12345.009, the fakedisplay will work normally as 12345.009, but the Display stuck as 12,345 until 9 is pressed, and at that time it will show 12,345.009 normally.
However, it is strange that when the user presses 0, there is no response, and until pressing 9, 009 will then immediately append.
I know this arise from the parsing code, but based on this, how could I amend the following code? I really cannot think of any solution... Many thanks for all your advice!
one.setOnClickListener(new View.OnClickListener() {
if (str.length()<15) {
Fakedisplay.append("1");
}
DecimalFormat myFormatter1 = new DecimalFormat("###,###,###,###.#################");
String str1=Fakedisplay.getText().toString();
String stripped1 = Double.valueOf(str1).toString();
stripped1 = myFormatter1.format(Double.valueOf(stripped1));
if (stripped1.endsWith(".0"))
stripped1 = stripped1.substring(0, stripped1.length() - 2);
Display.setText(stripped1);
}
Probably the easiest solution is to not strip off the .0 in the code for every keystroke..
Instead, only strip off trailing zeros (assuming there's a decimal point in there of course) when the user calls for a result. Entering keys such as the digit keys 0 through 9, the decimal point ., or the sign-change key +/- (what I'll call the entry keys) are not generating a result so should not strip trailing zeros.
However, non-entry keys, such as when you press + or - or = on your calculator can freely modify the number.
That will give you a display of the digits being entered as the user enters them but will still strip off trailing zeros when necessary.
You can do that with a modification to your statement (and, as mentioned, only doing this when the user presses a non-entry key):
stripped1 = stripped1.replaceAll("(\\.[0-9]*[1-9])0+$","$1");
stripped1 = stripped1.replaceAll("\\.0$","");
The first statement removes all trailing zeros at the end of a decimal number (other than on if it's really an integer). The second takes care of that case.
No doubt I could make a single substitution if I gave it some more thought but that should be enough to get it functional.