Archive

Archive for February, 2010

How to Sort a Two Dimensional String Array using JAVA

February 15th, 2010 No comments

Recently I had to write a program to sort data (String) in a table and was looking for an efficient and easiest way to do sorting in java. I found two options – one was to write my own sorting program, and the other was to use the Comparator class in the Util package. I found that using Comparator or Comparable interface was very easy compared to writing my own sorting algorithm.

If you are new to using “Comparator”, I highly recommend reading this one page tutorial.
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Here is a working example for sorting a two dimensional string array:

import java.util.Arrays;
import java.util.Comparator;

public class Sort {	
	public static void main(String args[]){
		//Array with address to sort
		String[][] addressArray = new String[][]{
				{"E","abc st","Sunshine", "FL"},
				{"S","abc st","Sunrise", "FL"},
				{"E","abd st","Cleveland", "OH"},
				{"N","aab st","Dallas", "TX"},
				{"W","xyz st","San Antonio", "TX"}};
				
		//Sort the array by city name or column 3
		Arrays.sort(addressArray, new ColumnComparator(2));
		
		//Print the sorted array
		for(int i=0; i<addressArray.length; i++){
			String[] row = addressArray[i];
			for(int j=0; j<row.length; j++) {
				System.out.print(row[j] + " | ");
			}
			System.out.print("\n");
		}
	}
}
//Class that extends Comparator
class ColumnComparator implements Comparator {
	int columnToSort;
	ColumnComparator(int columnToSort) {
		this.columnToSort = columnToSort;
	}
	//overriding compare method
	public int compare(Object o1, Object o2) {
		String[] row1 = (String[]) o1;
		String[] row2 = (String[]) o2;
		//compare the columns to sort
		return row1[columnToSort].compareTo(row2[columnToSort]);
	}
}

Results – sorted by city name

E | abd st | Cleveland | OH | 
N | aab st | Dallas | TX | 
W | xyz st | San Antonio | TX | 
S | abc st | Sunrise | FL | 
E | abc st | Sunshine | FL | 
Share
Categories: Tech Tips Tags: , ,

How to match AdSense colors with the WordPress theme?

February 9th, 2010 1 comment

Ever wondered how to display ads from Google Adsense that will blend in with your wordpress theme and colors, rather than having the standard white and blue that will shadow everything else you have on your site. I read articles from different bloggers, which suggested that a site with AdSense in matching theme would have a better click-rate compared to ones with the standard colors. I thought of trying this, and listed below are the steps I followed.

In order to change the colors, you have to generate the adsense ad units with colors that match your site. Go to Adsense Setup > Get Ads > Adsense for Content > Ad Unit > Colors
adsense_color_palette

Google will allow you to edit each of the fields in this default color palette, but the trouble is to identify matching colors by looking at your stylesheet. All the colors and styles will be defined in “syle.css”, and you will be able to access this file by going to Dashboard > Appearance > Editor or download the theme and look for the style.css.  In the example below, I am trying to match adsense colors to the sidebar of wordpress theme “iNova”.

Background
In syle.css search for “background:” look for the block were you want your ads to appear.
sidebar { background:#F7F7F7 url(img/sidebar_shadow.gif) top repeat-x;
}
This is just an example. Different themes will have different way of defining background colors.

Border
Keep the color same as background so that border will be invisible.

URL
Look for the style definition for anchor tags
a { color:#2970A6;
}

Text
For the side panel, keep it the same color as the URL (varies for theme).

Title
Look for the color definition for anchor tags
title a { color:#FFFFFF;
}

Font
Look for font definitions like – “font-family:”
body {
font-family:Verdana,”BitStream vera Sans”,Tahoma,Helvetica,Sans-serif;
font-size:12px;
}

Here is the final color palette that match WordPress iNove theme sidebar.
adsense_color_palette_iNove




If you know of any better ways to match colors please feel free to comment.

Share

How to increase server timeout in eclipse?

February 4th, 2010 No comments

If you are running JBOSS or other servers within eclipse, and your application takes more than 50 seconds to launch, then you might see a similar exception – “Server JBOSS was unable to start within 50 seconds. If the server requires more time try increasing the timeout in the server editor”

To fix this timeout in eclipse (Galilieo), go to Window -> Show View -> Servers -> Click on server name -> Increase start timeout.
eclipse_server_view

If you are using an older eclipse build (Europa or below), then go to Window -> Preferences -> Server

Share
Categories: Tech Tips Tags: , ,