Results 1 to 7 of 7
  1. #1
    xpatiencex is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default Errors,errors and errors

    I am having a hard time compiling and running this program can someone help.




    import java.io.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.net.*;

    class NewsItem
    {
    NewsItem(String h, String s)
    {
    headline = h;
    summary = s;
    }

    string headline;
    String summary;
    }

    /** A dialog box to demonstrate use of the GridBagLayout.
    */
    public class Sports extends Frame
    {
    String espn = "sports.espn.go.com/espn/rss/news";

    String channelName;
    int numItems = 0;
    newsItem[] news = new NewsItem[100];

    String getTextBetween(String source, String startMarker, String endMarker)
    {
    int s = source.indexOf(startMarker);
    if (s < 0)
    return "";
    int e = source.indexOf(endMarker, s);
    if (e < 0)
    return "";
    String text = source.substring(s + startMarker.length(), e);

    if (text.startsWith("<![CDATA[") && text.endsWith("]]>"))
    text = text.substring(9, text.length()-3);
    return text;
    }

    public void readNewsFeed(String urlString)
    {
    String data = "";

    BufferedReader in = null;
    try
    {
    URL url = new URL(urlString);
    InputStream inStream = url.openStream();
    in = new BufferedReader(new InputStreamReader(inStream));

    String s = in.readLine();
    while (s != null)
    {
    data += s;
    s = in.readLine();
    }
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    try { in.close(); } catch (Exception ex) { }
    }

    channelName = getTextBetween(data, "<description>", "</description>");

    numItems = 0;
    int x = data.indexOf("<item>");
    while (x >= 0 && numItems < 100)
    {
    data = data.substring(x);
    news[numItems] =
    new NewsItem(
    getTextBetween(data, "<title>", "</title>"),
    getTextBetween(data, "<description>", "</description>");
    numItems++;
    x = data.indexOf("<item>", 6);
    }
    }

    Label channelLabel = new Label("");
    Label headlinesLabel = new Label("Headlines:");
    List headlinesList;
    Label summaryLabel = new label("Summary:");
    JLabel summaryText = new JLabel("<html>(double-click on a headline)</html>");
    Button closeButton = new Button("Close);

    public Sports()
    {
    super("News Feed Reader");
    setSize(500, 300);
    setLocation(50, 50);

    readNewsFeed(espn);
    headlinesList = new List(numItems);
    int i;
    for (i=0; i<numItems; i++)
    headlinesList.add(news[i].headline);

    setLayout(new GridBagLayout());

    channelLabel = new Label(channelName);

    add(channelLabel,
    gbc(0, 0, 2, 1,
    GridBagConstraints.HORIZONTAL,
    GridBagConstraints.WEST,
    0.0, 0.0));

    add(headlinesLabel,
    gbc(0, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST,
    0.0, 0.0));

    add(headlinesList,
    gbc(0, 2, 1, 1,
    GridBagConstraints.BOTH,
    GridBagConstraints.WEST,
    0.5, 1.0));

    add(summaryLabel,
    gbc(1, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST,
    0.0, 0.0));

    add(summaryText,
    gbc(1, 2, 1, 1,
    GridBagConstraints.BOTH,
    GridBagConstraints.WEST,
    1.0, 1.0));

    add(closeButton,
    gbc(1, 3, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.EAST,
    0.0, 0.0));

    headlinesList.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    int i = headlinesList.getSelectedIndex();
    summaryText.setText("<html>" + news[i].summary + "</html>");
    }
    }
    );

    closeButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    System.exit(0);
    }
    }
    );

    setVisible(true);
    }

    /** A helper method for creating GridBagConstraints objects.
    */
    static GridBagConstraints gbc(
    int x, int y, int width, int height, int fill, int anchor, double weightx, double weighty)
    {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    gbc.fill = fill;
    gbc.anchor = anchor;
    gbc.weightx = weightx;
    gbc.weighty = weighty;
    gbc.insets = new Insets(3, 3, 3, 3);
    return gbc;
    }

    public static void main(String[] args)
    {
    Sports ex = newSports();
    }
    }

  2. #2
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    use tags next time...i checked ur code and found this errors..correct them..then post what other errors u get


    Java Code:
    Button closeButton = new Button("Close);
    Java Code:
    Sports ex = newSports();
    Java Code:
    new NewsItem(
    getTextBetween(data, "<title>", "</title>"),
    getTextBetween(data, "<description>", "</description>");

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    also if you want help with particular errors, you need to post the error messages you get. looking at the post before mine, you should learn how to decipher compiler messages as well. you have a bunch of typos and it's not the most productive use of your time to wait for someone to tell you to balance parentheses, quotation marks, etc.

  4. #4
    xpatiencex is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default Errror

    I noticed some of my typos I think I fix most of them but still say I have one error when compiled.

    The error am getting now say that class Sports is public, should be declared in a file named Sport.java


    public class Sports extends Frame


    I am still new to all of this so am not sure what this really mean when my class should be declared in a file named Sports.java

    When I save this program it was save automatically for me under NewsItem.java

    So does it mean that the program name should of been saved under Sports.java instead of NewsItem.java?







    import java.io.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.net.*;

    class NewsItem
    {
    NewsItem(String h, String s)
    {
    headline = h;
    summary = s;
    }

    String headline;
    String summary;
    }

    /** A dialog box to demonstrate use of the GridBagLayout.
    */
    public class Sports extends Frame
    {
    String espn = "//sports.espn.go.com/espn/rss/news";

    String channelName;
    int numItems = 0;
    NewsItem[] news = new NewsItem[100];

    String getTextBetween(String source, String startMarker, String endMarker)
    {
    int s = source.indexOf(startMarker);
    if (s < 0)
    return "";
    int e = source.indexOf(endMarker, s);
    if (e < 0)
    return "";
    String text = source.substring(s + startMarker.length(), e);

    if (text.startsWith("<![CDATA[") && text.endsWith("]]>"))
    text = text.substring(9, text.length()-3);
    return text;
    }

    public void readNewsFeed(String urlString)
    {
    String data = "";

    BufferedReader in = null;
    try
    {
    URL url = new URL(urlString);
    InputStream inStream = url.openStream();
    in = new BufferedReader(new InputStreamReader(inStream));

    String s = in.readLine();
    while (s != null)
    {
    data += s;
    s = in.readLine();
    }
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    try { in.close(); } catch (Exception ex) { }
    }

    channelName = getTextBetween(data, "<description>", "</description>");

    numItems = 0;
    int x = data.indexOf("<item>");
    while (x >= 0 && numItems < 100)
    {
    data = data.substring(x);
    news[numItems] =
    new NewsItem(
    getTextBetween(data, "<title>", "</title>"),
    getTextBetween(data, "<description>", "</description>"));
    numItems++;
    x = data.indexOf("<item>", 6);
    }
    }

    Label channelLabel = new Label("");
    Label headlinesLabel = new Label("Headlines:");
    List headlinesList;
    Label summaryLabel = new Label("Summary:");
    JLabel summaryText = new JLabel("<html>(double-click on a headline)</html>");
    Button closeButton = new Button("Close");

    public Sports()
    {
    super("News Feed Reader");
    setSize(500, 300);
    setLocation(50, 50);

    readNewsFeed(espn);
    headlinesList = new List(numItems);
    int i;
    for (i=0; i<numItems; i++)
    headlinesList.add(news[i].headline);

    setLayout(new GridBagLayout());

    channelLabel = new Label(channelName);

    add(channelLabel,
    gbc(0, 0, 2, 1,
    GridBagConstraints.HORIZONTAL,
    GridBagConstraints.WEST,
    0.0, 0.0));

    add(headlinesLabel,
    gbc(0, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST,
    0.0, 0.0));

    add(headlinesList,
    gbc(0, 2, 1, 1,
    GridBagConstraints.BOTH,
    GridBagConstraints.WEST,
    0.5, 1.0));

    add(summaryLabel,
    gbc(1, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST,
    0.0, 0.0));

    add(summaryText,
    gbc(1, 2, 1, 1,
    GridBagConstraints.BOTH,
    GridBagConstraints.WEST,
    1.0, 1.0));

    add(closeButton,
    gbc(1, 3, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.EAST,
    0.0, 0.0));

    headlinesList.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    int i = headlinesList.getSelectedIndex();
    summaryText.setText("<html>" + news[i].summary + "</html>");
    }
    }
    );

    closeButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    System.exit(0);
    }
    }
    );

    setVisible(true);
    }

    /** A helper method for creating GridBagConstraints objects.
    */
    static GridBagConstraints gbc(
    int x, int y, int width, int height, int fill, int anchor, double weightx, double weighty)
    {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    gbc.fill = fill;
    gbc.anchor = anchor;
    gbc.weightx = weightx;
    gbc.weighty = weighty;
    gbc.insets = new Insets(3, 3, 3, 3);
    return gbc;
    }

    public static void main(String[] args)
    {
    Sports ex = new Sports();
    }
    }

  5. #5
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by xpatiencex View Post
    I noticed some of my typos I think I fix most of them but still say I have one error when compiled.

    The error am getting now say that class Sports is public, should be declared in a file named Sport.java


    public class Sports extends Frame


    I am still new to all of this so am not sure what this really mean when my class should be declared in a file named Sports.java

    When I save this program it was save automatically for me under NewsItem.java

    So does it mean that the program name should of been saved under Sports.java instead of NewsItem.java?
    USE the CODE tags if u want help...btw u have many typos.....and yes the filename should be Sports.java

    p.s. i see that u haven't wrotte the program alone, but its copy/paste...try first to learn the basic elements of JAVA, it will be much easier for u ...
    Last edited by tomiu; 04-24-2009 at 11:59 PM.

  6. #6
    xpatiencex is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you I went and did the changes and also change the file name to Sports.java and it worked. You are correct this program was copy/paste so I can learn to figure out where the errors were and make the correction that is needed.

    Just for future use what do u mean by use the "CODE" tags?
    Can u give an example?

    Thank you

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Similar Threads

  1. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 08:05 AM
  2. Awww Errors!!!
    By AWPtic in forum New To Java
    Replies: 22
    Last Post: 03-26-2009, 07:00 AM
  3. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  4. makeButton errors
    By ljk8950 in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2008, 01:10 AM
  5. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •