1

What I am trying to do is to extract the all of the currency values, and their names from this website. And store them in two different arrays.

The only thing I found that relates to my problem is this code:

public static void main(String[] args) throws IOException {
    String url = "";
    Document doc = Jsoup.connect(url).get();
    Element content = doc.select("").first();
}

But I'm having a hard time putting it in a for loop, in order for the program to find all of the values and their names, and finding the right element, which goes in the doc.select(element) part, since I don't really know HTML.

So what I'm looking for is something like this:

Two different methods, one that finds the currency value, and one that finds its name, and both methods puts their values in the right array.

The most important thing is finding the right element, since I can do the rest by myself. How can I find the right element for me to extract the values from?

1 Answer 1

0

You can use Jsoup to extract all expected data from provided website. Firstly you need to select all table rows using following selector:

table.ratesTable > tbody > tr

Next for each row you have to select td elements (each row has 3 table cells) and you can assign values from those cells to any variables you want. Your code could look like this one:

Document doc = Jsoup.connect("http://www.x-rates.com/table/?from=USD&amount=1").get();

Elements rows = doc.select("table.ratesTable > tbody > tr");

for (Element row : rows) {
    Elements tds = row.select("td");

    String currency = tds.get(0).text();
    Double rate1 = Double.valueOf(tds.get(1).text());
    Double rate2 = Double.valueOf(tds.get(2).text());

    System.out.printf("Currency: %s, rate1: %s, rate2: %s%n", currency, rate1, rate2);
}

This program produces following console output:

Currency: Euro, rate1: 0.845569, rate2: 1.182635
Currency: British Pound, rate1: 0.768631, rate2: 1.301014
Currency: Indian Rupee, rate1: 64.134227, rate2: 0.015592
Currency: Australian Dollar, rate1: 1.266043, rate2: 0.789863
Currency: Canadian Dollar, rate1: 1.267137, rate2: 0.789181
Currency: Singapore Dollar, rate1: 1.360245, rate2: 0.735162
Currency: Swiss Franc, rate1: 0.96141, rate2: 1.040139
Currency: Malaysian Ringgit, rate1: 4.298091, rate2: 0.232661
Currency: Japanese Yen, rate1: 109.110914, rate2: 0.009165
Currency: Chinese Yuan Renminbi, rate1: 6.661812, rate2: 0.150109
Currency: Argentine Peso, rate1: 17.74499, rate2: 0.056354
Currency: Australian Dollar, rate1: 1.266043, rate2: 0.789863
Currency: Bahraini Dinar, rate1: 0.377148, rate2: 2.651478
Currency: Botswana Pula, rate1: 10.354673, rate2: 0.096575
Currency: Brazilian Real, rate1: 3.194036, rate2: 0.313084
Currency: Bruneian Dollar, rate1: 1.360245, rate2: 0.735162
Currency: Bulgarian Lev, rate1: 1.662835, rate2: 0.601383
Currency: Canadian Dollar, rate1: 1.267137, rate2: 0.789181
Currency: Chilean Peso, rate1: 646.170017, rate2: 0.001548
Currency: Chinese Yuan Renminbi, rate1: 6.661812, rate2: 0.150109
Currency: Colombian Peso, rate1: 2973.600291, rate2: 3.36E-4
Currency: Croatian Kuna, rate1: 6.248125, rate2: 0.160048
Currency: Czech Koruna, rate1: 22.130355, rate2: 0.045187
Currency: Danish Krone, rate1: 6.332396, rate2: 0.157918
Currency: Euro, rate1: 0.845569, rate2: 1.182635
Currency: Hong Kong Dollar, rate1: 7.8192, rate2: 0.12789
Currency: Hungarian Forint, rate1: 257.81906, rate2: 0.003879
Currency: Icelandic Krona, rate1: 105.635027, rate2: 0.009467
Currency: Indian Rupee, rate1: 64.134227, rate2: 0.015592
Currency: Indonesian Rupiah, rate1: 13357.499908, rate2: 7.5E-5
Currency: Iranian Rial, rate1: 32849.5, rate2: 3.0E-5
Currency: Israeli Shekel, rate1: 3.586513, rate2: 0.278822
Currency: Japanese Yen, rate1: 109.110914, rate2: 0.009165
Currency: Kazakhstani Tenge, rate1: 332.672501, rate2: 0.003006
Currency: South Korean Won, rate1: 1143.773759, rate2: 8.74E-4
Currency: Kuwaiti Dinar, rate1: 0.302319, rate2: 3.307765
Currency: Libyan Dinar, rate1: 1.37005, rate2: 0.7299
Currency: Malaysian Ringgit, rate1: 4.298091, rate2: 0.232661
Currency: Mauritian Rupee, rate1: 33.552148, rate2: 0.029804
Currency: Mexican Peso, rate1: 17.835986, rate2: 0.056066
Currency: Nepalese Rupee, rate1: 102.352456, rate2: 0.00977
Currency: New Zealand Dollar, rate1: 1.366238, rate2: 0.731937
Currency: Norwegian Krone, rate1: 7.93064, rate2: 0.126093
Currency: Omani Rial, rate1: 0.384499, rate2: 2.600784
Currency: Pakistani Rupee, rate1: 105.360213, rate2: 0.009491
Currency: Philippine Peso, rate1: 51.055889, rate2: 0.019586
Currency: Polish Zloty, rate1: 3.627166, rate2: 0.275697
Currency: Qatari Riyal, rate1: 3.642217, rate2: 0.274558
Currency: Romanian New Leu, rate1: 3.871704, rate2: 0.258284
Currency: Russian Ruble, rate1: 59.885203, rate2: 0.016699
Currency: Saudi Arabian Riyal, rate1: 3.751954, rate2: 0.266528
Currency: Singapore Dollar, rate1: 1.360245, rate2: 0.735162
Currency: South African Rand, rate1: 13.467078, rate2: 0.074255
Currency: Sri Lankan Rupee, rate1: 153.070914, rate2: 0.006533
Currency: Swedish Krona, rate1: 8.140123, rate2: 0.122848
Currency: Swiss Franc, rate1: 0.96141, rate2: 1.040139
Currency: Taiwan New Dollar, rate1: 30.365433, rate2: 0.032932
Currency: Thai Baht, rate1: 33.201131, rate2: 0.030119
Currency: Trinidadian Dollar, rate1: 6.7275, rate2: 0.148644
Currency: Turkish Lira, rate1: 3.539571, rate2: 0.28252
Currency: Emirati Dirham, rate1: 3.674458, rate2: 0.272149
Currency: British Pound, rate1: 0.768631, rate2: 1.301014
Currency: Venezuelan Bolivar, rate1: 9.977002, rate2: 0.100231

I hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect !! @SzymonStepniak Thank you so much!
@OmerHen Cool, I'm glad I could help you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.