Portlet Factory, Version 6.1.2


 

A Translate Method Implementation

When the users enter a phone number, they may enter it without any separators or use some sort of separator such as dots or dashes. You can use a translate method to alter user input into a format expected by the data. The following shows a translate method, which is quite similar to the format method in that it examines the expression passed to it and calls the corresponding operation that actually translates the user input into the valid data format.

public String translate(String value, String expression) { String result = null; if (expression.equals(PHONEDATA)) { result = stripPhoneNumber(value); return result;
}

As in the case for the format method, the translate method checks to see if the expression equals one of the pre-defined expression constants and calls a method supplying the value as its argument. The following code example shows the stripPhoneNumber method:

private String stripPhoneNumber(String value) { String result = value; String temp = value; if(value.indexOf('.') > -1) { temp = value.trim(); StringBuffer sb = new StringBuffer(temp); for(int i= sb.length() - 1;i > -1;i--){ char c = sb.charAt(i); if (c == '.'){ sb.deleteCharAt(i); result = sb.toString();
} //end if else if (value.indexOf('-') > -1) { StringBuffer sb = new StringBuffer(temp); for(int i= sb.length() - 1;i > -1;i--){ char c = sb.charAt(i); if (c == ' ' || c == '(' || c == ')' || c == '-'){ sb.deleteCharAt(i);
} //end if result = sb.toString(); return result;
}

Parent topic: About creating a formatter class


Library | Support |