Friday, June 15, 2007

How to count html links in a web page

We can use parse the hmtl pages of a site and then further write a recursive program to count the no of html links present in that web page.
Here is the code snippet that will give you an idea.

Suppose your web page content is given as a String object in java

This program parses the above html page and will return you two html links present in the web page as follows.

2
http://localhost/mypage1.html
http://localhost/mypage2.html

2 is the total count of html links present in web page and links are given at each
line.

Following is the program that gives us total html links and their count




Modifications can be done to this code so as to incorporate hashmap instead of arraylist. This may be helpful if we want to count only unique links present in the page.

When we use hashmap, we can use this program to recursively crawl the website's each
page as each link will be unique in hashmap and we can fetch pages corresponding to those links using standard java techniques and hence recursively applying the program for newly obtained page content.

Sphere: Related Content

Monday, June 11, 2007

Formatting a number to a fixed number of decimal places

We can use a DecimalFormat object to format a given number to a fixed number of places.
Here is the example that does the trick

import java.text.DecimalFormat;
import java.text.ParseException;

public class TestClass {
public static void main(String[] args) throws ParseException {
double doubleAmount_ = 10000;

DecimalFormat currency = new DecimalFormat("$0.00 dollars");

// Without using DecimalFormat class and hence without using fixed no. of decimal places.
Float result = (float) doubleAmount_ / 66;
System.out.println("$" + result + " dollars");
// Now using DecimalFormat class and hence fixing no.of decimal places to two.
String formattedResult = currency.format(result);
System.out.println(formattedResult);

// parsing a float value from a String using a DecimalFormat class.
String strAmount = "$10000.00 dollars";
Float floatValue = currency.parse(strAmount).floatValue();
System.out.println(floatValue);
}
}

Here are the results:

$151.51515 dollars
$151.52 dollars
10000.0

In the constructor of DecimalFormat we pass the format string, according to which the input value will be formatted.
I have demonstrated two ways in which divison is performed. First one is normal divison and hence leads to unformatted
output 151.51515, but in second the division is formatted upto two decimal places using format method of DecimalFormat
class.

Further we can use DecimalFormat class to parse a number from a formatted string like done above. In this case we use
parse method of DecimalFormat class. this gives us the output of 10000.0 when given an input string "$10000.00 dollars".

We can typically encapsulate the DecimalFormat object within a class, so that each time we do not need to pass the DecimalFormat
class instance. Typically we use toString method to format the results.

Using this model, here's the example

public class TestClass {

public static void main(String[] args) {
double totalAmount = 10000.0;
DecimalFormatTest amount = new DecimalFormatTest(totalAmount / 66);
System.out.println(amount);
}
}


class DecimalFormatTest {
double amount_;

public DecimalFormatTest(double amount) {
amount_ = amount;
}

public String toString() {
DecimalFormat currency = new DecimalFormat("$0.00 dollars");
return currency.format(amount_);
}
}


It produces the following output
$151.52 dollars

Sphere: Related Content