Click here to Skip to main content
15,938,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I included the main method in a simple java project :

public static void main(String[] args) {
       /* public void run() {
                createAndShowGUI();
            }**/
        });

and it returned this error:
...\ComputeNumbers.java:82: class, interface, or enum expected

public static void main(String[] args) {<br />
              ^<br />
1 error

Process completed.


any idea what i am not getting right?
thanks, shasha
Posted
Updated 2-Dec-10 3:07am
v2

1 solution

The method main has to be inside a class definition.
You'll need to wrap a class around that:

public class MyClass
{
    public static void main(String[] args)
    {
        //do your stuff here
    }
}


Modification:
Since the error seems to be on line 82. You may want to check if you
somehow went past the class definition to place your main method.

Cheers


Manfred
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 3-Dec-10 2:59am    
Moved OP's "answer2 to comment:
thanx manfred,
but can i define another class and wrap it around that main method? and yes i think i went past the class definition and brought the main method to the end of the coding... or i just have to make sure i everything (variables and other methods) are within the main method?

ps.
public class computeNumbers extends JFrame implements ActionListener {

is my main class definition
Manfred Rudolf Bihy 3-Dec-10 3:07am    
It really depends what you're trying to do in that main program. Since it looks like you're writing some Swing application, I'd guess that you want to place the code that ramps up your GUI stuff inside that method.
For that I would use a separate class like my.name.space.Program and do all the stuff one needs to do to initialize the GUI.
shasha mamiya 3-Dec-10 5:42am    
hi, thanks a lot for your help, but i think since i am still new at this, i am getting a bit more uncertain; can you please take a look at this entire code again? i haven't added the compute methods yet, just want to get the GUI running and figure out the rest later... thanks again!

//edwin s. mamiya...
//short program to compute numbers...

import java.awt.*;
import java.awt.event.*; //java package for handling events such as user actions on buttons...
import javax.swing.*;
import java.util.*;
//import java.io.*; //if we are gonna work with file IO later with our little program, say saving to notepad...
//import java.sql; //...or database via ms access or (my)sql...


public class computeNumbers extends JFrame implements ActionListener {

public void JFrame(){

//declaring our labels, fields, etc...
JLabel label1 = new JLabel("First Number", JLabel.RIGHT);
JLabel label2 = new JLabel("Second Number", Jlabel.RIGHT);
JButton buttonCalculate = new JButton("Compute"); // the calculate button
buttonCalculate.setToolTipText("Click here to compute the numbers");
JButton buttonReset = new JButton("Reset"); // reset button...
buttonReset.setToolTipText("Click here to clear the fields");

JTextField numField1 = new JTextField(6); //this will only tolerate a 6 digit number input from the user...
//numField1.addKeyListener(this);
JTextField numField2 = new JTextField(6); //same as above...
//numField2.addKeyListener(this);

//numField1.setFocusTraversalKeysEnabled(false); //should we disable the TAB, SHIFT-TAB keys?
//numField2.setFocusTraversalKeysEnabled(false); //same as we did for numbField1 above?

JPanel contentPanel = new JPanel(); //introduce the main panel for the contents...
SCLayout scl = new SCLayout(2, SCLayout.FILL, SCLayout.FILL, 3); //the positioning of the contents...
scl.setScale(0, 1.5);
contentPanel.setLayout(scl);

//now in the display panel we can add our content...
JPanel displayPanel = new JPanel();
SGLayout sgl = new SGLayout(3, 2, SGLayout.LEFT, SGLayout.CENTER, 5, 0); //the positioning of the displays on the content...
sgl.setColumnScale(0, 0.4);
sgl.setColumnAlignment(0, SGLayout.RIGHT, SGLayout.CENTER); //... and their alignments...
displayPanel.setLayout(sgl);
displayPanel.add(new JLabel ("First Number"));
displayPanel.add(label1);
displayPanel.add(new JLabel ("Second Number"));
displayPanel.add(label2);

//we create a panel to house our two buttons...
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
SRLayout srl = new SRLayout(3, SRLayout.FILL, SRLayout.CENTER, 10);
srl.setMargins(5, 15, 5, 15);

buttonPanel.setlayout(srl);
buttonPanel.add(buttonCalculate);
buttonCalculate.addActionListener(this);//this will listen to the calculate button...
buttonPanel.add(buttonReset);
buttonReset.addActionListener(this);//do for the reset...

contentPanel.add(displayPanel);
contentPanel.add(buttonPanel);
setContentPane(contentPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close the frame if user exits...
pack(); //pack everything on this panel...

}
public void actionPerformed(ActionEvent evt){
// Object obj = evt.getSource();
if (evt.getSource() == buttonCalculate){
//what should happen here if the user clicks on the Compute Button?
/**hence will include our method to compute the numbers entered by the user...*/
}
}

/** if (evt.getSource() == buttonReset){ //clear any entries made in the fields on Reset...
Reset rst = new Reset(); //??? is that really it?
reset(); //or is it this one?
}*/

public static void main(String[] args) {
/* public void run() {
createAndShowGUI();
}**/
});
}
Manfred Rudolf Bihy 3-Dec-10 5:56am    
You have to rework the part after public actionPerformed(ActionEvent evt). There seems to be a superflous closing parenthesis ")" at the end. Please use an editor supporting java syntax, that will help you to find matching parenthesis and stuff. (Notepad++ is good for that).
shasha mamiya 4-Dec-10 8:47am    
thanks again manfred! will see what comes out after this advice... good day!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900