文字列変換を使用して、URLから変数(FLOAT)にデータを割り当てます
質問
特定のURLからデータをダウンロードする外国為替レートアプリを開発し、各通貨とその値を別の通貨で識別するISOコードを含む文字列を検索しています。私はこれをPythonで開発しましたが、Javaに移植し、その後Androidに移植しようとしています。
Pythonコード:urllibを使用する(2)
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])
.
.
.
上記のコードは、PythonおよびAndroidのSL4Aで正しく機能的であり、JavaとAndroid SDKでそれを移植しようとしています。
これまで、私はこれを標準のJavaで書いています:
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();
}
}
それでは、フロート変数のURLに描かれた値を取得するために、文字列操作を適用するにはどうすればよいですか?
私の英語でごめんなさい。
解決
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;
これがあなたにいくつかの洞察を与えることを願っています
他のヒント
StringBuilderとIndexof()メソッドをご覧ください。
所属していません StackOverflow