Use enum with number - android

I know there are many alternatives to reach what I wish, but I wont this solution because it is the most comfortable to me. I wish to use enum that starts with number, like so.
public enum Quality {
1080p,
720p,
BlueRay //this one OK
}
And then use it like so when converting to string:
Quality.1080p.name();
Why it is not possible?

Because the Java language doesn't allow variable names to start with a number- just a letter or underscore. Any character after the first may be a number. The main reason for this is to make parsing easier, and prevent situations where the parser can't tell if a symbol is a number or a variable name.
For example, if numbers were valid at the start of a variable I could do the following:
String 1 = "string";
System.out.println(1);
Does this print 1 or "string"? They avoid the problem by not allowing it. Many (most?) languages have that restriction.

Related

How to revert back from currency format, Android?

I'm developing Android app which uses this method:
public static String currencyFormat(BigDecimal n) {
return NumberFormat.getCurrencyInstance().format(n);
}
which formats number based on Locale currency.
How to revert back from currency format, e.g. $35, to 35? Note that I cannot just remove First character because different locales have different currency name lengths.
You must store the currency unit in a separate field, encapsulated into some higher-order abstraction such as a final value class (with appropriate equals and hashcode defined), eg called CurrencyAmount. [if you do scala, basically you want a case class]. Any other solution will require you to 'reverse engineer' the unit portion from the amount portion and depending on the complexity of your spec of allowable values, it might be reliable only to various degrees. I would just encode the two portions in their own fields and solve this for all cases.
You might try to cut out all non numeric characters from a String with a regex.
Try this.

Correct Value(s) to store in TAG_GPS_PROCESSING_METHOD

Thanks for reading this question. I am sure the experts on this site will be able to provide the help I need.
I am trying to write an app which allows users to edit the exif information of the photos on their Android Phone.
As a part of improved user experience, I want to apply data validation where ever possible.
For the Exif Tag - TAG_GPS_PROCESSING_METHOD I am not able to apply the validation correctly.
Here is the part of code that I have applied :
String strGPSProc = etGPSProc.getText().toString();
if(strGPSProc.equalsIgnoreCase("GPS") || strGPSProc.equalsIgnoreCase("CELLID") || strGPSProc.equalsIgnoreCase("WLAN") || strGPSProc.equalsIgnoreCase("MANUAL") ) {
returnValue = true;
}else {
returnValue=false;
showToast("Incorrect value for GPS Processing Method. Correct value options are GPS, CELLID, WLAN or MANUAL.");
etGPSProc.requestFocus();
}
This code checks if the value entered in the EditText meant for GPSProcessingMethod, has any one of the four prescribed value as described in the documentation of EXIF.
But when I try to save this using setAttribute() and saveAttributes() functions, a non catch-able exception appears in logcat.
Unsupported encoding for GPSProcessingMethod
I understand from Exif Documentation that values for GPSProcessingMethod needs to be stored with some header information.
I need some expert advise on how to implement this correctly, with out using any other 3rd part classes.
Accoridng to the Exif specification:
GPSProcessingMethod
A character string recording the name of the method used for location finding. The first byte indicates the character
code used (Table 6、Table 7), and this is followed by the name of the method. Since the Type is not ASCII, NULL
termination is not necessary
Atually, Table 6 lists the character codes as 8 byte sequences, so the above should probably read "The first bytes indicate...". Anyway, the character code designation for ASCII is defined as 41.H, 53.H, 43.H, 49.H, 49.H, 00.H, 00.H, 00.H., Unicode is (unsurprisingly) 55.H, 4E.H, 49.H, 43.H, 4F.H, 44.H, 45.H, 00.H. I guess these should be all you need.
Hope that helps.
EDIT:
Just discovered that ExifInterface.setAttribute() only supports String values... You could try encoding the value at the beginning of your string, but I doubt that would work. Sounds like the encoding should be handled by the setAttribute() or saveAttributes() method. Could it be a bug in the API? I had a look at the source code, but the actual writing of values is done by native code so I stopped digging further.

Scan string for characters and return bounded text

I am writing a dictionary-type app. I have a list of hash-mapped terms and definitions. The basic premise is that there is a list of words that you tap on to see the definitions.
I have this functionality up and running - I am now trying to put dynamic links between the definitions.
Example: say the user taps on an item in the list, "dog". The definition might pop up, saying "A small furry [animal], commonly kept as a pet. See also [cat].". The intention is that the user can click on the word [animal] or [cat] and go to the appropriate definition. I've already gone to the trouble of making sure that any links in definitions are bounded by square brackets, so it's just a case of scanning the pop-up string for text [surrounded by brackets] and providing a link to that definition.
Note that definitions can contain multiple links, whilst some don't contain any links.
I have access to the string before it is displayed, so I guess the best way to do this is to do the scanning and ready the links before the dialog box is displayed.
The question is, how would I go about scanning for text surrounded by square brackets, and returning the text contained within those brackets?
Ideally the actual dialog box that is displayed would be devoid of the square brackets, and I need to also figure out a way of putting hyperlinks into a dialog box's text, but I'll cross that bridge when I come to it.
I'm new to Java - I've come from MATLAB and am just about staying afloat, but this is a less common task than I've had to deal with so far!
You could probably do this with a regular expression; something like this:
([^[]*)(\[[^]]+\])
which describes two "match groups"; the first of which means any string of zero or more characters that aren't "[" and the second of which means any string starting with "[", containing one or more characters that aren't "]", and ending with "]".
Then you could scan through your input for matches to this pattern. The first match group is passed through unchanged, and the second match group gets converted to a link. When the pattern stops matching your input, take whatever's left over and transmit that unchanged as well.
You'll have to experiment a little; regular expressions typically take some debugging. If your link text can only contain alphanumerics and spaces, your pattern would look more like this:
([^[]*)(\[[\s\w]+\])
Also, you may find that regular expression matching under Android is too slow to be practical, in which case you'll have to use wasyl's suggestion.
Quite simple, I think... As the text is in brackets, you need to scan every letter. So the basic recipe would be :
in a while loop scan every character (let's say, while i < len(text))
If scanned character is [:
i++;
Add letter at index i to some temporary variable
while (character # i) != ']' append it to the temporary variable
store this temporary variable in a list of results.
Some tips:
If you use solution above, use StringBuilder to append text (as regular string is immutable)
You might also want (and it's better, I think) to store starting and ending positions of all square brackets first, and then use string.substring() on each pair to get the text inside. This way you'd first iterate definition to find brackets (maybe catch unmatched ones, for early error handling), then iterate pairs of indices...
As for links, maybe this will be of use: How can I get clickable hyperlinks in AlertDialog from a string resource?

Partial comparison of 2 strings

I'm looking for a way to compare 2 strings partial. I need to clear this with an example.
The base string is "equality".
The string I need to check is spelled wrong: "equallaty". I want to conform this is partially correct so the input, even not right in a grammar way, is the same as the base string.
Now I can of course parse the string to an char array. Now I can check every single character, but if I check the first 4 characters they will be right, the rest will be wrong even if there are only 2 mistakes. So the check I want to use is that a minimum of 70 procent of the characters should match.
Is anyone able to help me get on the right track?
Compare the strings with an edit-distance metric like the Levenshtein distance. Such a metric basically counts the number of changes needed to make the strings equal. If the number of changes is small relative to the total size of the string, then you can consider the strings similar.

How Match Regular expression of international number in android

i have a EditText in my activity where i take a phone number from user.. Now user provides a number activity and based on xml rules you i have to tell whether application routes the phone number or not.
This is more of a java question, you need to know how to build regular expressions. So I assume you know how to fetch the value of the edit text. Lets assume you have loaded the phone number in a String phoneNumber. So now how do you check against a regular expression:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
....
Pattern pattern = Pattern.compile("\+0[8-9][0-9]{2}-[0-9]{2}-[0-9]{3}");
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
// The phone number matches the template given. do the routing.
}
In th eexample I have given I am searching for phone numbers that start with + (note that + is a special character for regexes, thus I need to escape it, the same holds for $, ., ^), then I expect a zero, 8 or 9, then exactly 2 digits, a dash 2 more digits and 3 more digits. The if matcher.matches() will return true only if the phoneNumber is exactly of the described format. Hopefully this will give you a brief introduction to the regex power of java.
ITU: National Numbering Plans can help you with the harder problem.
Your question is poor enough that any one of my link, Boris's regex-centric answer, or an answer that focused on nothing more than Android's GUI could be what you're really looking for. Please keep this in mind for future questions.

Categories

Resources