Monday, April 7, 2008

JAVA@ Removing Duplicate Records from ArrayList

The following code removes the duplicate records from the ArrayList of Java.

//////////////////////////////////////

import java.util.ArrayList;

public class RemoveDuplicateArrayElement {
public static void main(String [] as)
{
// String [] temp={"sqqa","as","sa","sd","sa","fd","sa","sa","sa"};
ArrayList aryDuplicate=new ArrayList();

aryDuplicate.add(new String("sabah"));
aryDuplicate.add(new String("irfan"));
aryDuplicate.add(new String("waqas"));
aryDuplicate.add(new String("sabah"));
aryDuplicate.add(new String("sabah"));
aryDuplicate.add(new String("Ahmad"));
// for(int i=0;i
// {
// if(listContains(aryL,temp[i]))
// {
// // This is already in the List, Don't push it.
// //System.out.println("List already contains"+temp[i]);
// }
// else
// {
// aryL.add(new String(temp[i]));
// }
// }
// aryDuplicate1=aryDuplicate;//
//
// for(int j=0;j
// {
// //System.out.println(j+":"+(String)aryDuplicate1.get(j));
// }

aryDuplicate=RemoveDuplicates(aryDuplicate);
System.out.println(aryDuplicate.size());
for(int j=0;j
{
System.out.println(j+":"+(String)aryDuplicate.get(j));
}
//System.out.println("List contains = "+listContains(aryL,"sfa") );
}
/////////////////////////////////////

public static ArrayList RemoveDuplicates(ArrayList aryLst)
{
ArrayList noDuplicate=new ArrayList();
for(int i=0;i
{

if(listContains(noDuplicate,(String)aryLst.get(i)))
{
//This is already in the List, Don't push it.
// System.out.println((String)aryLst.get(i));
}
else
{
noDuplicate.add(new String((String)aryLst.get(i)));

}
}

return noDuplicate;
}
/////////////////////////
public static boolean listContains(ArrayList aryList, String str)
{
boolean result=false;

for(int i=0;i
{
// System.out.println((String)aryList.get(i));
if(str.equalsIgnoreCase((String)aryList.get(i)))
{
result= true;
break;
}
else
{
result= false;
}

}

return result;
}
///////////////////////////////////

public static boolean listContains(String[] aryList, String str)
{
boolean result=false;

for(int i=0;i
{
// System.out.println((String)aryList.get(i));
if(str.equalsIgnoreCase((String)aryList[i]))
{
result= true;
break;
}
else
{
result= false;
}

}

return result;
}


} // end class

Friday, March 14, 2008

JAVAScript: Function To Validate UK ZipCodes

// Method that validates the Basic Business Rules for UK Zip codes.

function checkPostCode(pc) { //check postcode format is valid
var test = pc;
var size = test.length
test = test.toUpperCase(); //Change to uppercase
while (test.slice(0,1) == " ") { //Strip leading spaces
test = test.substr(1,size-1);size = test.length
}
while(test.slice(size-1,size) == " ") { //Strip trailing spaces
test = test.substr(0,size-1);size = test.length
}
if (size == 0) {
return "please enter a valid postcode";
}
//document.details.pcode.value = test; //write back to form field
if (size <> 8){ //Code length rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - wrong length";
}
if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - cannot start with a number";
}
if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
// return "please enter a valid postcode";
return test + " is not a valid postcode - alpha character in wrong position";

}
if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - number in wrong position";
}
if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - number in wrong position";
}
if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - space in wrong position";

}
count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
if (count1 != count2){//only one space rule
return "please enter a valid postcode";
return test + " is not a valid postcode - only one space allowed";
}
return "OK";
}

Wednesday, March 12, 2008

JAVAScript: StringTokenizer Function

String.prototype.tokenize = tokenize;

function tokenize()
{
var input = "";
var separator = " ";
var trim = "";
var ignoreEmptyTokens = true;

try {
String(this.toLowerCase());
}
catch(e) {
window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);");
return;
}

if(typeof(this) != "undefined")
{
input = String(this);
}

if(typeof(tokenize.arguments[0]) != "undefined")
{
separator = String(tokenize.arguments[0]);
}

if(typeof(tokenize.arguments[1]) != "undefined")
{
trim = String(tokenize.arguments[1]);
}

if(typeof(tokenize.arguments[2]) != "undefined")
{
if(!tokenize.arguments[2])
ignoreEmptyTokens = false;
}

var array = input.split(separator);

if(trim)
for(var i=0; i
{
while(array[i].slice(0, trim.length) == trim)
array[i] = array[i].slice(trim.length);
while(array[i].slice(array[i].length-trim.length) == trim)
array[i] = array[i].slice(0, array[i].length-trim.length);
}

var token = new Array();
if(ignoreEmptyTokens)
{
for(var i=0; i
if(array[i] != "")
token.push(array[i]);
}
else
{
token = array;
}

return token;
}
/////////////////////////////////