rpo rpo - 6 months ago 27
Java Question

Getting java variable's value from URL contents

For learning purpose i am trying to write a simple currency converter. I want to get the updated rate from Google.

public void Google() throws IOException {
String url="https://www.google.com/finance/converter?a=1&from=USD&to=BDT";
URL theUrl=new URL(url);
URLConnection openUrl=theUrl.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader(openUrl.getInputStream()));
String result=null;
while ((result=input.readLine()) != null){



It gets me the html source:

<div id=currency_converter_result>1 USD = <span class=bld>77.9284 BDT</span>

So i only need the rate 77.9284 BDT and store it in a variable.

I am not getting any idea how to do it! Do i need somekind of regex?

Any help will be appreciated !


To extract the DOM elements effectively, you can use jsoup library to parse the html content.

Please use the below code snippet (import org.jsoup package at class level) for your requirement:

    public void google() throws IOException {
        Document doc = Jsoup.connect("https://www.google.com/finance/converter?a=1&from=USD&to=BDT").get();
        Element element = doc.getElementById("currency_converter_result");
        String text = element.text();