Advertising (This ad goes away for registered users. You can Login or Register)

Assign data from a url to a variable (float) - Java

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
andfoy
Posts: 20
Joined: Fri Nov 19, 2010 4:11 pm
Location: Somewhere in my dreams

Assign data from a url to a variable (float) - Java

Post by andfoy » Sat Jan 14, 2012 7:33 pm

Assign data from a url to a variable (float) using string transformation

I'm developing a foreign exchange rate app that downloads data from a specific url and then search for a string that contains an ISO code that identifies each currency and its value in another currency. I've developed this in python, but I'm trying to port it to Java and subsequently to Android.

Python Code: Using urllib(2)

Code: Select all

import urllib
import urllib2
from urllib import urlencode
from urllib2 import urlopen
from encodings import utf_8
import os

pg = urlopen("http://rss.timegenie.com/forex.txt")
text = pg.read().decode("utf8")

usd = text.find('USD')
start_of_priceusd = usd + 25
end_of_priceusd = start_of_priceusd + 6
priceusd = float(text[start_of_priceusd:end_of_priceusd])
.
.
.
The above code is correct and functional on Python and in SL4A in Android, now i'm trying to port that in Java and the Android SDK.

So far I've written this in standard Java:

Code: Select all

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {
  URL curr = new URL("hhttp://rss.timegenie.com/forex.txt");
  BufferedReader in = new BufferedReader(
        new InputStreamReader(
        curr.openStream()));

  String inputLine;

  while ((inputLine = in.readLine()) != null)
      System.out.println(inputLine);

  in.close();
    }
}
I tried this to get the index and then take steps:

Code: Select all

List<Float> priceList = new ArrayList<Float>();
     while ((inputLine = in.readLine()) != null)
         System.out.println(inputLine);
         String usd = inputline.substring(inputline.indexOf("USD")+25,inputline.length);     
         Float f = Float.valueOf(usd);
         priceList.add(f);              
        }
      in.close();

return priceList; 
But instead i'm getting an exception:

Code: Select all

Exception in thread "main" java.lang.NullPointerException
	at URLReader.main(URLReader.java:18)
So, how can i apply string manipulation to get the values depicted in the URL in a float variable?


Sorry for my English.
Advertising
"Die Luft der Freiheit weht"
"El hombre no piensa como vive sino que vive como piensa"
PSP 3003 -> 6.20 PRO-XX
PC: Dual boot 7/Ubuntu
Linux FTW!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Assign data from a url to a variable (float) - Java

Post by m0skit0 » Sat Jan 14, 2012 8:00 pm

Please format your code correctly:

Code: Select all

import java.net.*;
import java.io.*;

public class URLReader {
    
    public static void main(String[] args) throws Exception {
    
  		URL curr = new URL("hhttp://rss.timegenie.com/forex.txt");
  
  		BufferedReader in = new BufferedReader(
        	new InputStreamReader(
        	curr.openStream()));

  		String inputLine;

  		while ((inputLine = in.readLine()) != null)
      		System.out.println(inputLine);

  		in.close();
    }
}
Also please avoid * in imports. Import only what you're actually gonna use.

Are you sure that URL is correct? Also how can it say "URLReader.main(URLReader.java:18)" when in your original code there's no line 18? Are you sure this is the code? You recompiled it?

EDIT: also, before doing curr.openStream(), you should check if curr is not null...
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

andfoy
Posts: 20
Joined: Fri Nov 19, 2010 4:11 pm
Location: Somewhere in my dreams

Re: Assign data from a url to a variable (float) - Java

Post by andfoy » Sun Jan 15, 2012 2:48 pm

Thanks for the response, actually there were 2 problems.
First it was the url, it was http:// instead of hhttp:// :D , the other problem was that when the URLReader got the URL, it reads for the time that the stream open making curr "null", so i assigned a StringBuilder that writes the stream in each iteration, and then i applied the search method to get the values.

Muchas gracias por las pistas :lol:
"Die Luft der Freiheit weht"
"El hombre no piensa como vive sino que vive como piensa"
PSP 3003 -> 6.20 PRO-XX
PC: Dual boot 7/Ubuntu
Linux FTW!

Post Reply

Return to “Programming”