Tuesday, June 16, 2009

Convert an array to collection

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;

public class ArraysExample
public static void main(String[] args)
String[] array = {"Happy", "New", "Year", "2006"};
List list = Arrays.asList(array);

Iterator iterator = list.iterator();
while (iterator.hasNext())
System.out.println((String) iterator.next());

The result of our code is:


ref: http://www.kodejava.org/examples/25.html

Thursday, June 11, 2009

How do I add AdSense to my google site?

Now, ... Google Site has new Gadget for AdSense. So, ... anyone can add your own adsense if you have existing adsense's account

If you want to add Google AdSense to your site, just follow these steps:

  1. Click the More actions drop-down menu, and select Manage site.
  2. Click Monetize on the left side of the page.
  3. Click the Monetize this site button.
  4. Create a new AdSense account or use an existing account. Follow the instructions, depending on the radio button you select.

Once you've enabled AdSense, go to any of your site's pages, click Insert, and select AdSense to insert ads within your page. You can also choose to have ads displayed in your global sidebar.

AdSense can appear at the side bar by using edit side bar and page content by using insert.

Have a fun ^^"

ref: https://www.google.com/adsense/support/bin/answer.py?hl=en&answer=48182

Tuesday, June 9, 2009

Get number of rows in resultset

You can extract the data from the resultset from the top and rs.last() will move you to the end of the resultset.

Then, the method rs.getRow() get the row number of the last row and also shows you the number of rows in the table.

For example:

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
String query = "select [table_field] from [table_name]";
stmt = conn.createStatement();

rs = stmt.executeQuery(query);
while (rs.next()) {
String id = rs.getString(1);
int rowCount = rs.getRow();
System.out.println("Number of Rows=" + rowCount);
} catch (Exception e) {
} finally {
try {
} catch (SQLException e) {}

ref: http://www.roseindia.net/java/java-get-example/number-rows-resul.shtml

Thursday, June 4, 2009

java BigDecimal problem

BigDecimal v = new BigDecimal(0.12d);

result: 0.11999999999999999555910790149937383830547332763671875

Because ::: In Java API

public BigDecimal(double val)

Translates a double into a BigDecimal. The scale of the BigDecimal is the smallest value such that (10scale * val) is an integer.

Note: the results of this constructor can be somewhat unpredictable.


But ...

BigDecimal v = new BigDecimal("0.12");

result: 0.12


The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=15

java decimal using BigDecimal

BigDecimal v = new BigDecimal(0.0d);
BigDecimal av = new BigDecimal(0.1d);
av = av.setScale(2, RoundingMode.HALF_EVEN);
for (int i = 0; i < 10; i++) {
v = v.add(av);

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0

java decimal format

java.text.DecimalFormat dfm = new java.text.DecimalFormat("0.00");

double d = 0.123d;
System.out.println(dfm.format(d)); // 0.12

d = 0.129d;
System.out.println(dfm.format(d)); // 0.13

d = new Double(dfm.format(d)).doubleValue();
System.out.println(d); // 0.13

OR ...

DecimalFormat changeFormat = new DecimalFormat("#,##0.00");

double a = 20213243;

BigDecimal aa = new BigDecimal(a);

BigDecimal divideA = aa.divide(new BigDecimal(3),4,4);

System.out.println("divideA = "+divideA);
System.out.println("Result = "+changeFormat.format(divideA));

divideA = 6737747.6667
Result = 6,737,747.67

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0

Wednesday, June 3, 2009

java double problem

public class HOWCOME
public static void main(String[] args) {
double v = 0.0d;
for (int i = 0; i < 10; i++) {
v += 0.1d;

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0

MIT Java Wordnet Interface

JWI (the MIT Java Wordnet Interface) is an easy-to-use, easy-to-extend Java library for interfacing with Wordnet. JWI supports access to Wordnet versions 1.6 through 3.0. Wordnet is a freely and publicly available semantic dictionary of English, developed under the direction of George Miller at Princeton University.

here is url: http://projects.csail.mit.edu/jwi/