can someone tell me what is going on
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class FindPrimesextends JFrameimplements Runnable{
privatestaticfinalint FRAME_WIDTH = 400;
privatestaticfinalint FRAME_HEIGHT = 300;
private JLabel label;
private JPanel panel;
private JTextField textField;
private JTextArea textArea;
private JButton button;
private Thread go;
publicstaticvoid main(String[] args){
FindPrimes frame =new FindPrimes();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public FindPrimes(){
Container contentPane = getContentPane();
label =new JLabel("Quantity");
button =new JButton("Display");
textField =new JTextField("400", 10);
textArea =new JTextArea(8,40);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLayout(new BorderLayout());
panel.add(label);
panel.add(textField);
panel.add(button);
contentPane.add(panel, BorderLayout.NORTH);
JScrollPane pane =new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
ActionListener buttonListener =new ActionListener(){
publicvoid actionPerformed(ActionEvent evt){
button.setEnabled(true);
if(go==null){
go =new Thread(this);
go.start();
}
};
};
button.addActionListener(buttonListener);
publicvoid run(){
int quantity = Integer.ParseInt(textField.getText());
int numPrimes = 0;
// candidate: the number that might be prime
int candidate = 2;
textArea.append("First " + quantity +" primes:");
while (numPrimes < quantity){
if (isPrime(candidate)){
textArea.append(candidate +" ");
numPrimes++;
}
candidate++;
}
}
publicstaticboolean isPrime(int checkNumber){
double root = Math.sqrt(checkNumber);
for (int i = 2; i <= root; i++){
if (checkNumber % i == 0){
returnfalse;
}
}
returntrue;
}
}
}
these are the errors.
C:\Java programs\swing\FindPrimes.java:43: illegal start of expression
publicvoid run(){
^
C:\Java programs\swing\FindPrimes.java:57:';' expected
};
^
2 errors
Can someone tell me what in the world is going on?
Message was edited by:
lrngjava
Message was edited by:
lrngjava
[5402 byte] By [
lrngjavaa] at [2007-11-27 4:56:20]

You have a missing closing brace for the actionPerformed method.
Consistant indentaion would have made this easier to locate.
Ack! Make that the constructor missing the closing brace.
no flounder. in my other swing programs i write my actionlistener method like this and works fine.
public void actionPerformed(ActionEvent e) {
button.setText("IS cool");
text1.append("\n" + text.getText());
};
};
button.addActionListener(okListener);
but with this program its not working. if i enclose a closing brace i get an error for identifier expected at button.addActionListener.
I am sure something else is wrong
no constructor is missing no braces its fine.
In that case you have the run method inside the constructor!
See what I mean about indentation. It makes your code a nightmare to read and debug.
Great thankyou. one question the error makes sense. it says cannot find symbol Thread(anonymous java.awt.thread)
Since i have my actionPerformed method inside the constructor and the run method outside is it ok? or how can i get rid of that error.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class FindPrimes extends JFrame implements Runnable {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 300;
private JLabel label;
private JPanel panel;
private JTextField textField;
private JTextArea textArea;
private JButton button;
private Thread go;
public static void main(String[] args) {
FindPrimes frame = new FindPrimes();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public FindPrimes() {
Container contentPane = getContentPane();
label = new JLabel("Quantity");
button = new JButton("Display");
textField = new JTextField("400", 10);
textArea = new JTextArea(8,40);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLayout(new BorderLayout());
panel.add(label);
panel.add(textField);
panel.add(button);
contentPane.add(panel, BorderLayout.NORTH);
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setEnabled(true);
if(go==null) {
go = new Thread(this);
go.start();
}
};
};
button.addActionListener(buttonListener);
}
public void run() {
int quantity = Integer.ParseInt(textField.getText());
int numPrimes = 0;
// candidate: the number that might be prime
int candidate = 2;
textArea.append("First " + quantity + " primes:");
while (numPrimes < quantity) {
if (isPrime(candidate)) {
textArea.append(candidate + " ");
numPrimes++;
}
candidate++;
}
}
public static boolean isPrime(int checkNumber) {
double root = Math.sqrt(checkNumber);
for (int i = 2; i <= root; i++) {
if (checkNumber % i == 0) {
return false;
}
}
return true;
}
}
error
[code]
C:\Java programs\swing\FindPrimes.java:40: cannot find symbol
symbol : constructor Thread(<anonymous java.awt.event.ActionListener>)
location: class java.lang.Thread
go = new Thread(this);
^
C:\Java programs\swing\FindPrimes.java:50: cannot find symbol
symbol : method ParseInt(java.lang.String)
location: class java.lang.Integer
int quantity = Integer.ParseInt(textField.getText());
^
2 errors
[/code
> See what I mean about indentation. It makes your code> a nightmare to read and debug.Yes you are right flounder. sorry for any inconvenience.
go = new Thread(this);
That line is inside the actionPerformed method of the anonymous ActionListener class. Therefore this refers to the anonymous ActionListener class and not your FindPrimes class.
Time to rethink your design maybe.
i guess i will go like this.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FindPrimes extends JFrame implements Runnable, ActionListener {
Thread go;
JLabel howManyLabel = new JLabel("Quantity: ");
JTextField howMany = new JTextField("400", 10);
JButton display = new JButton("Display primes");
JTextArea primes = new JTextArea(8, 40);
FindPrimes() {
super("Find Prime Numbers");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout bord = new BorderLayout();
setLayout(bord);
display.addActionListener(this);
JPanel topPanel = new JPanel();
topPanel.add(howManyLabel);
topPanel.add(howMany);
topPanel.add(display);
add(topPanel, BorderLayout.NORTH);
primes.setLineWrap(true);
JScrollPane textPane = new JScrollPane(primes);
add(textPane, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
display.setEnabled(false);
if (go == null) {
go = new Thread(this);
go.start();
}
}
public void run() {
int quantity = Integer.parseInt(howMany.getText());
int numPrimes = 0;
// candidate: the number that might be prime
int candidate = 2;
primes.append("First " + quantity + " primes:");
while (numPrimes < quantity) {
if (isPrime(candidate)) {
primes.append(candidate + " ");
numPrimes++;
}
candidate++;
}
}
public static boolean isPrime(int checkNumber) {
double root = Math.sqrt(checkNumber);
for (int i = 2; i <= root; i++) {
if (checkNumber % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] arguments) {
FindPrimes fp = new FindPrimes();
}
}
thanks
