Jul 21, 2009

Solution for java puzzles(2)!

/*Provide declarations for the variables x and i such that*/

public class Tweedledum {
public static void main(String[] args) {
// Put your declarations for x and i here
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL
}
}
solution:
// Put your declarations for x and i here
int x=0;
byte i;

Solution for Java puzzles(1).

/*Provide a declaration for Enigma that makes the program print false.*/

public class TestClass{
public static void main(String[] args) {
Enigma e = new Enigma();
System.out.println(e.equals(e));
}
}

final class Enigma {
// Provide body that makes TestClass print false.
// Do n't override equals method in object class.
}
:Solution
final class Enigma {
public Enigma(){
System.out.print("false");
System.exit(0);
}
}
Any other solution will be appreciated ...

Jul 18, 2009

How to read Excel file in Java with jxl.jar?

/*Reading Excel file (with default locale) with jxl.jar file ,you can use file object to read file in workbook settings*/
public void processExcelFile(InputStream fileInput)throws Exception {
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
Workbook workbook = Workbook.getWorkbook(fileInput, ws);
//Workbook workbook = Workbook.getWorkbook(file, ws);
//Cell[] headCells = sheet.getRow(0);
for (row = 1; row < sheet.getRows(); row++) {
String errorStr = "";
Cell[] cells = sheet.getRow(row);
System.out.println("column1 value:"+cells[0].getContents());
System.out.println("column2 value:"+cells[1].getContents());
System.out.println("column3 value:"+cells[2].getContents());
}
}
To download jxl.jar file go through this link
http://www.docjar.com/jar/jxl-2.6.jar

Mar 22, 2009

How to Sort HashMap or Map in java?

/* Sorting Map and return LinkedHashMap, u can send HashMap implemented Object but it should a map like
Map map = new HashMap();
ma p = sortHashMapByValues(map);
*/
public LinkedHashMap sortHashMapByValues(Map passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);

LinkedHashMap sortedMap =
new LinkedHashMap();

Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();

while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();

if (comp1.equals(comp2)){
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String)key, val);
break;
}

}

}
return sortedMap;
}

Mar 18, 2009

Sql server pagination query

select* from ( SELECT ROW_NUMBER() OVER (ORDER BY ja.AppName) AS RowNumber, ja.* from Applicant ja )a where a.RowNumber <= uplt and a.RowNumber > lowlt