String Pattern matching in Blackberry - blackberry

How i can do a simple pattern matching in blackberry OS 6.0. The purpose is to check whether the user name entered to the UserName edit field contains special characters.... plz help me
thanks jibysthomas

A better solution would be to control the user input by adding an appropriate TextFilter to your edit field. That has the added benefit of modifying the on-screen keyboard to match your filter on those devices so equipped.
Here is an example combining the action of two built in text filters to make one that only allows upper letters and numbers:
import net.rim.device.api.ui.text.TextFilter;
import net.rim.device.api.system.Characters;
/**
* A TextFilter class to filter for station identifiers
*/
private static class StationFilter extends TextFilter {
// Order of the supporting filters is important, NUMERIC will convert
// letters to numbers if it gets them first.
private static TextFilter[] _tf = {
TextFilter.get(TextFilter.NUMERIC),
TextFilter.get(TextFilter.UPPERCASE)
};
// Convert using the first supporting filter that has a conversion
public char convert( char character, int status) {
char c = 0;
for (int i = _tf.length - 1; i >= 0; i--) {
c = _tf[i].convert(character, status);
if (c != 0) {
return c;
}
}
return 0;
}
// Validate a space for separator, then by supporting filter
public boolean validate(char character) {
if (character == Characters.SPACE) {
return true;
}
for (int i = _tf.length - 1; i >= 0; i--) {
boolean b = _tf[i].validate(character);
if (b) {
return true;
}
}
return false;
}
}

Related

What is the union of bitfield equivalent in Dart?

I'd like to achieve the following C code in Dart:
union AuxiliaryHardwares {
uint16_t all = 0;
struct {
hasRs485: 1;
hasCanbus: 1;
hasRelay0: 1;
hasRelay1: 1;
};
};
AuxialiaryHardwares ah;
ah.all = 123; // Or any value I read from the network
if (ah.hasCanbus) {
// blah blah
}
if (ah.hasRelay0) {
// blah blah
}
There is no language support for accessing individual bits of an integer as an integer or boolean variable.
Traditionally, you'd write it yourself.
class AuxiliaryHardwares {
static const int _rs485Flag = 1;
static const int _canbusFlag = 2;
static const int _relay0Flag = 4;
static const int _relay1Flag = 8;
int all = 0;
bool get hasRs45 => all & _rs45Flag != 0;
bool get hasCanbus => all & _canbusFlag != 0;
bool get hasRelay0 => all & _relay0Flag != 0;
bool get hasRelay1 => all & _relay1Flag != 0;
// Setters too if you want them, e.g.
void set hasRs45(bool value) {
all = value ? (all | _rs45Flag) : (all & ~_rs45Flag);
}
}
You can probably find a bit-set class somewhere which abstracts over accessing individual bits of an integer, but it'll be extra overhead for very little gain.
I expect that "inline classes" will be the future way to encapsulate integers like this.

InputFilter not behaving correctly

I have the following input filter on my Xamarin.Android app. When created, it sets if the input is caps only, alpha only, numeric only, alpha with separators etc - it's fairly flexible. The code is a direct port of some Java code found on here.
public ICharSequence FilterFormatted(ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend)
{
if (source is SpannableStringBuilder)
{
var sourceAsSpannableBuilder = (SpannableStringBuilder)source;
for (var i = end - 1; i >= start; i--)
{
if (!isCharacterOk(source.CharAt(i)))
{
sourceAsSpannableBuilder.Delete(i, i + 1);
}
}
return source;
}
else
{
var filteredStringBuilder = new SpannableStringBuilder();
for (int i = start; i < end; i++)
{
var currentChar = source.CharAt(i);
if (isCharacterOk(currentChar))
{
filteredStringBuilder.Append(currentChar);
}
}
return filteredStringBuilder;
}
}
The isCharacterOK method checks if the character is correct or not (for example the filter for caps only checks if the character is Upper and if Alpha is set). It works happily.
The filter works fine for code going forward (for example, if I type ASDFGjhkl, only ASDFG show in the edittext).
The problem is when I press delete, the dest seems to still include hjkl which means I need to hit delete 5 times before the letter G is removed.
Have I stumbled on a Xamarin bug, an android oddity or is this correct behavior? It seems very strange that dest is somehow picking up characters not in the EditText widget.
you could change like this:
if (source is SpannableStringBuilder)
{
var sourceAsSpannableBuilder = (SpannableStringBuilder)source;
for (var i = end - 1; i >= start; i--)
{
if (!isCharacterOk(source.CharAt(i)))
{
sourceAsSpannableBuilder.Delete(i, i + 1);
//return the new SpannableStringBuilder.
sourceAsSpannableBuilder = new SpannableStringBuilder(sourceAsSpannableBuilder);
}
}
return sourceAsSpannableBuilder;
}
the effect like below:

Method declaration in java

I have a question regarding this code, you can see in some methods that there are comments with a return, that is because I think I have to use a return method instead of a void method. My teacher told me to transform them to a void class, but isn't a method which modifies field variables suposed to return something? I'm in doubt because sometimes my teacher seems to not know so much about programming or has some doubts so, thank for your help beforehand.
public class ArraysClass {
private int[] array;
private int arrayLength;
public ArraysClass() {
setArrayLength();
array = new int[arrayLength];
}
public int setArrayLength() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to set the length of the array:");
arrayLength = scanner.nextInt();
System.out.println();
return arrayLength;
}
public void fillArray() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arrayLength; i++) {
System.out.println("Type a number to fill position " + i);
array[i] = scanner.nextInt();
}
// return array;
System.out.println();
}
public void findNumber() {
Scanner scanner = new Scanner(System.in);
int tofind, position;
System.out.println("Enter a number to search it in the array:");
tofind = scanner.nextInt();
position = Arrays.binarySearch(array, tofind);
if (position < 0) {
System.out.println("We did not find your number.");
} else {
System.out.println("The number you typed is in the next position: " + position);
}
System.out.println();
}
public void fillMethod() {
Scanner scanner = new Scanner(System.in);
int tofill;
System.out.println("Enter a number to fill the entire array with:");
tofill = scanner.nextInt();
Arrays.fill(array, tofill);
System.out.println();
//return array;
}
public void Sortmethod() {
Arrays.sort(array);
//return array;
}
private void showArray() {
System.out.println("Showing the array...");
for (int i = 0; i < arrayLength; i++) {
System.out.println(array[i]);
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArraysClass arrayobj = new ArraysClass();
int choose;
do {
do {
System.out.println("1-Fill the array");
System.out.println("2-Find a number in the array");
System.out.println("3-Fill the entire array with a number");
System.out.println("4-Sort the array");
System.out.println("5-Show the array");
System.out.println("6-Exit");
System.out.println("Which one do you want to use?:");
choose = scanner.nextInt();
} while (choose < 1 && choose > 6);
switch (choose) {
case 1:
arrayobj.fillArray();
break;
case 2:
arrayobj.findNumber();
break;
case 3:
arrayobj.fillMethod();
break;
case 4:
arrayobj.Sortmethod();
break;
case 5:
arrayobj.showArray();
break;
case 6:
break;
}
} while (choose != 6);
}
}
In general, a method should return something if you need value from it. It is an approach used by some programmers to return a boolean even for do-only methods for success or failure or an int, for a status code. I do not follow these approaches. When I implement a method, I always ask myself how would I like to use that method. If I need a value from it, then it will have its type. Otherwise, it will be void. Let us see your methods:
setArrayLength: In general, from this name I would expect that you pass an int to it, representing the length and the method to be void. This is very common for setters, but here you are reading the actual value inside the method which is clearly inferior compared to having an int parameter, as your method will be useless if one wants to set the array length using a value not read from the console.
fillArray: I would expect this to be void, so I agree with its declaration, but again, the reading part should not be here.
findNumber: Should get the number to be found as a parameter and return an int, which represents its index, -1 if not found.
fillMethod: Should be void and should have an int parameter, which represents the value to be used to fill the array.
sortMethod: ok, maybe return the resulting array, but depends on your needs.
showArray: I would expect a PrintStream there, you will not necessarily output to System.out
General mistake: You mix methods with in/out operations to the console, the code is not general enough this way.

Comma separation in the Text Field in Blackberry

in my application i have a Custom text box with BasicEditField.FILTER_NUMERIC. When the user enter the value in the field the comma should be added to the Currency format .
EX:1,234,567,8.... like this.
In my code i tried like this.
protected boolean keyUp(int keycode, int time) {
String entireText = getText();
if (!entireText.equals(new String(""))) {
double val = Double.parseDouble(entireText);
String txt = Utile.formatNumber(val, 3, ",");// this will give the //comma separation format
setText(txt);// set the value in the text box
}
return super.keyUp(keycode, time);
}
it will give the correct number format... when i set the value in the text box it will through the IllegalArgumentException. I know BasicEditField.FILTER_NUMERIC will not allow the charector like comma(,)..
How can i achieve this?
I tried this way and it works fine...
public class MyTextfilter extends TextFilter {
private static TextFilter _tf = TextFilter.get(TextFilter.REAL_NUMERIC);
public char convert(char character, int status) {
char c = 0;
c = _tf.convert(character, status);
if (c != 0) {
return c;
}
return 0;
}
public boolean validate(char character) {
if (character == Characters.COMMA) {
return true;
}
boolean b = _tf.validate(character);
if (b) {
return true;
}
return false;
}
}
and call like this
editField.setFilter(new MyTextfilter());

Using scanner to read phrases

Hey StackOverflow Community,
So, I have this line of information from a txt file that I need to parse.
Here is an example lines:
-> date & time AC Power Insolation Temperature Wind Speed
-> mm/dd/yyyy hh:mm.ss kw W/m^2 deg F mph
Using a scanner.nextLine() gives me a String with a whole line in it, and then I pass this off into StringTokenizer, which then separates them into individual Strings using whitespace as a separator.
so for the first line it would break up into:
date
&
time
AC
Power
Insolation
etc...
I need things like "date & time" together, and "AC Power" together. Is there anyway I can specify this using a method already defined in StringTokenizer or Scanner? Or would I have to develop my own algorithm to do this?
Would you guys suggest I use some other form of parsing lines instead of Scanner? Or, is Scanner sufficient enough for my needs?
ejay
oh, this one was tricky, maybe you could build up some Trie structure with your tokens, i was bored and wrote a little class which solves your problem. Warning: it's a bit hacky, but was fun to implement.
The Trie class:
class Trie extends HashMap<String, Trie> {
private static final long serialVersionUID = 1L;
boolean end = false;
public void addToken(String strings) {
addToken(strings.split("\\s+"), 0);
}
private void addToken(String[] strings, int begin) {
if (begin == strings.length) {
end = true;
return;
}
String key = strings[begin];
Trie t = get(key);
if (t == null) {
t = new Trie();
put(key, t);
}
t.addToken(strings, begin + 1);
}
public List<String> tokenize(String data) {
String[] split = data.split("\\s+");
List<String> tokens = new ArrayList<String>();
int pos = 0;
while (pos < split.length) {
int tokenLength = getToken(split, pos, 0);
tokens.add(glue(split, pos, tokenLength));
pos += tokenLength;
}
return tokens;
}
public String glue(String[] parts, int pos, int length) {
StringBuilder sb = new StringBuilder();
sb.append(parts[pos]);
for (int i = pos + 1; i < pos + length; i++) {
sb.append(" ");
sb.append(parts[i]);
}
return sb.toString();
}
private int getToken(String[] tokens, int begin, int length) {
if (end) {
return length;
}
if (begin == tokens.length) {
return 1;
}
String key = tokens[begin];
Trie t = get(key);
if (t != null) {
return t.getToken(tokens, begin + 1, length + 1);
}
return 1;
}
}
and how to use it:
Trie t = new Trie();
t.addToken("AC Power");
t.addToken("date & time");
t.addToken("date & foo");
t.addToken("Speed & fun");
String data = "date & time AC Power Insolation Temperature Wind Speed";
List<String> tokens = t.tokenize(data);
for (String s : tokens) {
System.out.println(s);
}

Resources