Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Stock price of a symbol

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stock price of a symbol

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Stock price of a symbol

    Please copy the full text of the error message and post it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: June 23rd, 2013, 02:37 AM
  2. Little Help, Price Reset is not working
    By drkossa in forum AWT / Java Swing
    Replies: 1
    Last Post: January 14th, 2010, 06:00 PM