I m trying to get the current stock price of a symbol using socket but the program is getting terminated.
String yahoo = "finance.yahoo.com" ;
final int httpd = 80;
Socket sock = new Socket(yahoo, httpd);
BufferedWriter out = new BufferedWriter( new OutputStreamWriter(sock.getOutputStream() ) );
String cmd = "GET/d/q?s= " +symbol[j] +"\n";
out.write(cmd);
out.flush();
BufferedReader in =new BufferedReader(new InputStreamReader( sock.getInputStream() ) );
String s=null;
please help me out ..
--- Update ---
import java.io.*;
import java.net.*;
public class stockquotes {
public static void main(String a[]) throws IOException {
String[] symbol={"ibm"};
for (int j = 0; j<symbol.length ; j++)
try{
String yahoo = "finance.yahoo.com" ;
final int httpd = 80;
Socket sock = new Socket(yahoo, httpd);
BufferedWriter out = new BufferedWriter( new OutputStreamWriter(sock.getOutputStream() ) );
String cmd = "GET/d/q?s= " +symbol[j] +"\n";
out.write(cmd);
out.flush();
BufferedReader in =new BufferedReader(new InputStreamReader( sock.getInputStream() ) );
String s=null;
int i, k;
// pick out the stock price from the pile of HTML
// it's in bold, so get the number following "<b>"
while ( (s=in.readLine()) != null) {
if (s.length()<25) continue;
if ((i=s.indexOf(symbol[j].toUpperCase())) < 0) continue;
s=s.substring(i);
if ((i=s.indexOf("<b>")) < 0) continue;
k = s.indexOf("</b>");
s=s.substring(i+7,k);
System.out.println(symbol[j] +" is at "+s);
}
}catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
This is my full program