Exchange rate calculator - Part 1/2
HTTP download and reading files
This week you will make the first steps towards an exchange rate calculator.
You will download an exchange rate web page, and extract the information
needed. Please note that Aftenposten is a registered trademark and that
this program is for educational purposes only.
|
First you need to make a small program which downloads a web page and
outputs its content to the screen. (Not the actual page, just the HTML
code).
Hint: java.net.URL, URL.openStream(), InputStreamReader, BufferedReader
GetPage.java
|
|
Now you need to extract the exchange rates. Depending on what source
you use, this will look differently. I have used Aftenposten's "Valutakurser"
page from
(Select "Valutakurser" from the left side menu)
To extract only the interesting parts of this large HTML file you need
to find where table of information starts and ends, and how this is formatted.
I will help you with this: As of November 2001, only one line contains
the string <TABLE width=100%>. This is where the table starts.
You could easily find the end of the table by searching for </TABLE>.
Finally, each piece of information you need is preceded by the string sans-serif">, and followed by a <. Be aware that this will certainly change.
What you need to do is to read the file line by line and search for
these text strings. When you find the lines of information, you need to
extract the correct part of the line and convert the numbers to float or
double variables.
Hint: String.indexOf(...), String.substring(...) and Double.parseDouble(...)
ShowRate.java
|
|
If you want to extend this application, you could make it run in the
back ground, extracting exchange rates every hour or day. Perhaps a ticker
would be fun, and even more interesting when showing stock values... :)
|
|
|