Archive for the ‘Coding’ Category

Simple Sort Program in Java

Here is a simple sort program in Java. This program sorts the integer in an array.

package beam;

public class SimpleSort {

public void sortAndPrint(int[] iArr) {
/* define an array */
int[] iNerArr = new int[iArr.length];
int minItem = 0;
int minItemPos = -1;
int newItem = 0;
int iter=0;

for (int i = 0; i < iArr.length; i++) {
minItem = iArr[i];
minItemPos = -1;
for (int j = i; j < iArr.length; j++) {
newItem = iArr[j];
iter++;
/* if curritem gt newitem, interchange the values */
if (minItem > newItem) {
minItem = newItem;
minItemPos = j;

}
}

if (-1 != minItemPos) {
iArr[minItemPos] = iArr[i];
iArr[i] = minItem;
}

}

System.out.println(“total iterations ” + iter);
printArray(“after “, iArr);
}

/*method to print array. you can use what ever you want.*/
public void printArray(String str, int[] iArr) {
System.out.println(“————–(” + str + ” )—————–”);
System.out.print(“[");
for (int itemp = 0; itemp < iArr.length; itemp++) {
System.out.print(iArr[itemp] + “,”);
}
System.out.print(“]”);
System.out.println(“\n————–(” + str + ” )—————–”);
}

/**
* @param args
*/
public static void main(String[] args) {
SimpleSort ss = new SimpleSort();
int[] iArr = { 4, 5, 6, 2, 1 };
ss.printArray(“before”, iArr);
ss.sortAndPrint(iArr);

int[] iArr1 = { 2, 3, 4, 5, 6 };
ss.printArray(“before”, iArr1);
ss.sortAndPrint(iArr1);

int[] iArr2 = { -22, 1003, 2314, 1235, 132336 };
ss.printArray(“before”, iArr2);
ss.sortAndPrint(iArr2);

int[] iArr3 = { 1,2,4,3,2,1,6,3,2,4,3,5 };
ss.printArray(“before”, iArr3);
ss.sortAndPrint(iArr3);

}

}

Comments in Programming Languages

Quick Reference - Comments

Quick Reference - Comments

no persistent classes found for query class

I was getting the following exception in my web application which is using spring and hibernate.

“no persistent classes found for query class”

Solution :

1. Spring configuration : In applicationContext.xml, the hibernate mappings must have the class referred in the named query.

2. Hibernate : Check the named query which is using proper class name and package name of the Bean.

Use Collator for String Comparison in Java

Use java.text.Collator for String comparison in Java. This is very helpful when writing the Custom Comparators for your application. Even though String.compareTo() function is there, but it wont suffice the requirement of comparing the strings. JDK API suggests to use Collator.

Sample Usage :

public int compare(MyString str1,MyString str2){

Collator descCollator = Collator.getInstance();

return descCollator.compare(str1.getDescription(), str2.getDescription());
}

REST – Articles, API’s Info

I was looking for some REST related articles, then I found the following Information :

Articles on REST:
http://www.xfront.com/REST-Web-Services.html – The Author describes about the usage and best practices.

Difference between REST and SOAP:
http://www.petefreitag.com/item/431.cfm

Where to find REST implemented APIs:
Flickr API – http://www.flickr.com/services/api/
Yahoo API’s – http://developer.yahoo.com/
37Signals API’s – http://developer.37signals.com/

Resources on XML:
http://www.xfront.com/index.html

REST – Articles, API's Info

I was looking for some REST related articles, then I found the following Information :

Articles on REST:
http://www.xfront.com/REST-Web-Services.html – The Author describes about the usage and best practices.

Difference between REST and SOAP:
http://www.petefreitag.com/item/431.cfm

Where to find REST implemented APIs:
Flickr API – http://www.flickr.com/services/api/
Yahoo API’s – http://developer.yahoo.com/
37Signals API’s – http://developer.37signals.com/

Resources on XML:
http://www.xfront.com/index.html

Log4j Logging level

Apache Log4j Logging Level:

Remember : D I W E F

D – Debug
I – Info
W – Warn
E – Error
F – Fatal

D < I < W < E < F ( lower to higher level of logging level )

Related Links
Log4j Manual