Tuesday, November 27, 2007

Only digits and dots and nothing else

Here is the requirement where the input is a String and our program should find whether it contains characters like a ,b (or) any special characters.

It can accept only digits and dots and nothing else and return a boolean result.

public static boolean questionnarieSpecific(String input){

Pattern p = Pattern.compile("[^\\d\\.]");
Matcher m = p.matcher(input);
boolean b = false;
boolean result = true;

while(b = m.find()) {
result= false;
break;
}

return result;
}

Friday, November 23, 2007

Rounding large values and eliminating exponentials

  1. For a requirement (where input is passed as Double and output as String) like


1 Should be rounded to dollars. No decimals, even if user enters.
2. Exponentials should not be displayed

3. Have amount returned as String in US format with dollar symbol and comma seperated


public class Test {

public static void main(String[] args){
System.out.println("Result : " + toUSCurrencyFormat(Double.parseDouble ("1000000000000123.123")));
}

public static String toUSCurrencyFormat(Double amount){

if(amount!=null&&!(ApplicationConstants.COMMON_EMPTYSTRING).equals(amount.toString())) {

Long l=Math.round(amount);
amount=Double.parseDouble(l.toString());
System.out.println("Rounded Value: " + Double.parseDouble(l.toString()));
NumberFormat usNF = DecimalFormat.getInstance(new Locale("en","US",""));
return usNF.format(amount);
}
return null;
}
}

3.

public static String toUSCurrencyFormatWithDollar(Double amount){

if(amount!=null && !(ApplicationConstants.COMMON_EMPTYSTRING).equals(amount.toString())){

Long l=Math.round(amount);

amount=Double.parseDouble(l.toString());

NumberFormat n = NumberFormat.getCurrencyInstance(java.util.Locale.US);

return n.format(amount);

}

return null;

}

Tutorial links on Locale and java.lang package classes

Tutorial Link - I

Tutorial Link - II

Thursday, November 22, 2007

Suppressing comma in a string using java

public class StringCommaSuppresser {

public static void main(String[] args) {
System.out.println(formatToValue("5,000,000"));
}

public static Double formatToValue(String amount) {

String tempAmount = amount;
Double amnt = 0.0;
try{
if (tempAmount.contains(",")) {
amount = "";
do {
amount = amount.concat(tempAmount.substring(0, tempAmount.indexOf(",")));
tempAmount = (tempAmount.substring(tempAmount.indexOf(",") + 1));
} while (tempAmount.contains(","));
amount = amount.concat(tempAmount);
}
if (amount != null && !("").equals(amount)) {
amnt = Double.parseDouble(amount);
}
return amnt;
} catch(NumberFormatException e){
return 0.0;
}
}
}

Output:
5000000.0

The above approach is a primitive way of doing things. We can better implement the same functionality in a efficient way like

public static Double formatToValue(String amount) {
try{
Double resultantAmount = null;
if (amount != null && !(ApplicationConstants.COMMON_EMPTYSTRING).equals(amount) && amount.contains(",")) {
String result = amount.replaceAll(",","");
resultantAmount = Double.valueOf(result);
}else if(amount != null && !(ApplicationConstants.COMMON_EMPTYSTRING).equals(amount) && !amount.contains(",")) {
resultantAmount = Double.valueOf(amount);
}
return resultantAmount;
}catch(NumberFormatException e){
return 0.0;
}
}

class ApplicationConstants {
public static final String COMMON_EMPTYSTRING="";
}

Monday, November 19, 2007

Tutorial Links

Tutorial Link

Tuesday, November 13, 2007

For populating a drop-down based on another drop-down value

For populating a drop-down based on another drop-down value.
---------------------------------------------------------------------------------------------

We have 2 List objects namely A and B, contains different Bean objects each. All these Bean objects have a Long property in common (private Long amount).
List A contains 4 bean objects.
List B contains 20 bean objects which inturn contains other properties as well.
We need to fetch only the relevant 4 bean objects from List B that match the bean objects from List A. This match is based on the common Long property.
----------------------------------------------------------------------------------------------
Long code = objRequestForm.getTransactionDropDownValue();
List resultantTransactionList = new ArrayList();
List transactionCodeList = new ArrayList();
Map requestTransactionMap = PopulateApplnDataServlet.getRequestTransactionMap();
List requestTransactionList = null;

if(requestTransactionMap != null && !requestTransactionMap.isEmpty()) {
requestTransactionList = requestTransactionMap.get(code);
}

List requestTxnTypesList = getLstRequestTxnTypes();

if(requestTransactionList != null) {
for(RequestTransactionBean objRequestTransactionBean: requestTransactionList) {
Long requestTxnTypeKey = objRequestTransactionBean.getRequestTxnTypeKey();
transactionCodeList.add(requestTxnTypeKey);
}

for(RequestTxnTypesBean objRequestTxnTypesBean: requestTxnTypesList) {
if(transactionCodeList.contains(objRequestTxnTypesBean.getRequestTxnTypeKey())){
resultantTransactionList.add(objRequestTxnTypesBean);
}
}

for(RequestTxnTypesBean objRequestTxnTypesBean: resultantTransactionList) {
if(objRequestTxnTypesBean.getRequestTxnTypeKey().equals(TRASACTIONTYPEKEY)) {
objRequestForm.setRequestTransactionTypeKey(TRASACTIONTYPEKEY);
} else {
objRequestForm.setRequestTransactionTypeKey(GTEMApplicationConstants.ZEROVALUE);
}
}
objRequestForm.setLstRequestTxnTypes(resultantTransactionList);

} else {
objRequestForm.setRequestTransactionTypeKey(GTEMApplicationConstants.ZEROVALUE);
}
----------------------------------------------------------------------------------------------

Step - 1 : We are getting the first drop-down value and storing it in Long property called code.
Step - 2 : We are calling a Map which returns a List object which contains 4 bean objects of type RequestTransactionBean
Step - 3 : We are invoking getLstRequestTxnTypes() method which returns a List objects which contains 20 objects of type RequestTxnTypesBean.
Step - 4 : We are iterating requestTransactionList which contains objects of type RequestTransactionBean and fetch the Long property from RequestTransactionBean and store it in a new List transactionCodeList of type Long.
Step - 5 : We are iterating requestTxnTypesList which contains objects of type RequestTxnTypesBean. We are comparing whether transactionCodeList contains Long property from objects RequestTxnTypesBean. If successful then add the RequestTxnTypesBean object to a new List object.

Monday, November 05, 2007

segregating objects as per code value in a map

We fetch details from a table which has say 5 columns and populate it inside a look-up bean(ABCBean) object and add it to an arraylist(ABCList).

It has columns like display(the string to be displayed to the user), code (which is a not a unique value and helps in grouping the display values to be shown on screen).

Example: It has codes like screen-1, screen-2. For screen-1 and 2 we have multiple display values in database table.

display code
a screen-1
b screen-1
c screen-1
a screen-2
c screen-2
e screen-2

Now all these display values are in a single arraylist(ABCList). We now need to segregate these values as per the code and put it in a map.
The below code seperates and stores all the bean objects as per the code value inside a map.

public void segregateMethod(ABCList abcList){
Map abcMap = new TreeMap();
if(abcList != null) {
for(Iterator itr = abcList.iterator();itr.hasNext();) {
ABCBean abcBean = (ABCBean) itr.next();
if(abcBean != null) {
String code = abcBean.getCode();
List abcValues = (List)abcMap.get(code);
if(abcValues == null) abcValues = new ArrayList();
abcValues.add(abcBean);
abcMap.put(code, abcValues);
}
}
}
}