/*What declaration turns this loop into infinitive loop.*/
public class Looper {
public static void main(String[] args) {
// Place your declaration for i here
while (i == i + 1) {
}
}
}
Solution:
// Place your declaration for i here
double i = 1.0/0.0
Let us share our views in java. Not only java, we can also share examples and configurations on Spring, Hibernate, javascript, CSS and Sqlserver queries. This blog will help you in giving the complete information and resolving your complete java related problems like the problems in core java, servlets, jsp spring, hibernate. And also the web technologies which includes all css related problems(Web 2.0).and java script and the latest smart jquery java script frame work
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;
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 ...
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
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;
}
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;
}
Subscribe to:
Posts (Atom)