Monday, April 19, 2010

Parse File Using Scanner - Java Implementation

Code snippet to parse a file using java.util.Scanner class.





Usage: Call the tokenizeUsingScanner method and pass it the File object and the delimiter for the file.
Note: The pipe symbol has special meaning in a regex -- so you need to escape it.

Tuesday, April 13, 2010

List all files in a folder - Java Implementation

A code snippet that recursively reads all the files in a folder and put them in a list




Usage:
Call the readFolder method and pass it the absolute folder path as a string.

Sunday, April 4, 2010

Fibonacci Number Series - Java Implementation

Posting a code snippet to generate a list of fibonacci numbers:



Usage:
Pass the number of fibonacci numbers you want to generate as a parameter
Returns a list of fibonacci numbers

Monday, March 15, 2010

IE Debugging - Using Fiddler and IE Developer tools

I have been developing web applications for a long time and I have gone through the pain of debugging them by putting "alert" stmts after each line of code.

Until Firebug came to the rescue. God bless the Firebug team! Firebug is such a strong tool that I almost immediately dumped IE and did all my development on FireFox. Firebug's debugging capabilities, HTTP tracking, dynamic content editing, DOM inspection were just too good to believe. And, very frankly I used to curse IE if something worked on FF and not on IE, since I had to go back to the "alert" approach.

Not anymore! I had started using Fiddler some time back but didnt find it very useful as my application was on HTTPS. After the release of Fiddler2 I am able to track each and every request and response. I have also started using the bundled Developer Tools in IE8 (under Tools menu or F12) that allow me to debug my JS scripts, check my CSS, inspect my HTML, check my browser cache and profile my pages. However, the developer tools are clunky, not very intutive and keeps crashing, but you would get used to it!

Few tips on using Fiddler2 efficiently:
  • I was unable to track requests originating from localhost. I followed what the team at Fiddler suggested. It works, although if you still want to stick with localhost you can use it this way-
    https://localhost.:8080/myapp/.. ("dot" after localhost)

  • To inspect the request parameters, I prefer to use the "WebForms" section in the request area.

  • To check the response, you can use either the "Raw" or "XML" section in the response area.

Using Fiddler2 alongside the Developer Tools, I can use IE as my development browser, but I have got so used to Firebug now, that I am gonna stick with it for a while!

Monday, March 8, 2010

Convert Excel Columns to Number

A code snippet that contains a utility method to get the column number from a column character, e.g. A -> 0 or AA -> 26

public int getColumnCount(String column, int multiplyFactor){
int col=0;
int minusFactor = -10;
if(column.length()==1){
col = minusFactor + Character.getNumericValue(column.charAt(0));
}else{
multiplyFactor += minusFactor + Character.getNumericValue(column.charAt(0)) ;
col = 26 * multiplyFactor + getColumnCount(column.substring(1),multiplyFactor+25);
}
return col;
}

Usage:
int colCount = getColumnCount(String columnChar,1)

Hope this helps!

Friday, March 5, 2010

Convert Numbers To Excel Columns

Another code snippet that contains a utility method to get the column character from a column number, e.g. 0 -> A or 26 -> AA


public String convertColumnNumberToChars( int i ){
int iBase = 26;
String interConversion = Integer.toString(i, iBase).toUpperCase();

char[] ac = interConversion.toCharArray();
for( int j = 0; j < ac.length; j++ ) {
int arrLen = ac.length - j - 1;
char c = ac[j];
ac[j] = (char) ('A' - arrLen + Character.digit( c, iBase ));
}
return String.copyValueOf( ac );
}

Usage:
String colChar = convertColumnNumberToChars(int columnCount)


In the next post i will also put down a method to convert a column character to column number.

Friday, February 26, 2010

Java's equivalent to Javascript's eval()

I had a situation in my code where I needed to dynamically execute a method based on a String I received from some other method. The string specified the method name I had to execute.
The easiest option was to put in an "if...else" block and do string comparison. However, that's not the cleanest way. I was looking for a way in Java to imitate javascripts eval behavior.
Reflection came to my rescue. Here's how you can dynamically invoke methods in your code:

Example A - Invoking a method that does not have any parameter
Assume you need to call method methodA() in class com.examples.Myclass

Class c = Class.forName("com.examples.Myclass");
Object objMyclass = c.newInstance();
Method m = c.getMethod("methodA", null);
m.invoke(objMyclass , null);


Example B - Invoking a method that accepts a String parameter and returns a String
Assume you need to call method methodB(String param1) in class com.examples.Myclass

Class[] clazzez = new Class[1];
clazzez[0] = Class.forName("java.lang.String");
Object[] params= new Object[1];
params[0] = "Parameter Value";

Class c = Class.forName("com.examples.Myclass");
Object objMyclass = c.newInstance();
Method m = c.getMethod("methodB", clazzez);

String return = (String)m.invoke(objMyclass, params);


In both the examples you need to handle the appropriate exceptions. For more reference, refer to package Reflection APIs and you can also check out these examples.

Let me know if you get stuck somewhere or come up with some other challenges.