Results 1 to 7 of 7
Thread: Errors,errors and errors
- 04-24-2009, 06:20 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
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();
}
}
- 04-24-2009, 07:20 PM #2
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
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>");
- 04-24-2009, 09:19 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
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.
- 04-25-2009, 12:42 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
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();
}
}
- 04-25-2009, 12:56 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
Last edited by tomiu; 04-25-2009 at 12:59 AM.
- 04-25-2009, 01:06 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
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
- 04-25-2009, 05:43 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
Java Code:public SClass() {}
[ code ] and [/ code ] without spacing
Similar Threads
-
Errors.
By rocky in forum New To JavaReplies: 4Last Post: 04-09-2009, 09:05 AM -
Awww Errors!!!
By AWPtic in forum New To JavaReplies: 22Last Post: 03-26-2009, 08:00 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 02:51 AM -
makeButton errors
By ljk8950 in forum AWT / SwingReplies: 12Last Post: 08-10-2008, 02:10 AM -
help with these errors
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-16-2008, 05:55 PM
Bookmarks