0

I'm new to Java programming and doing this as a project for school. Here's the code I've written

public static void main(String args[])
import javax.swing.*;
import java.awt.event.*;
public class SleepCounter extends JFrame {
  private JPanel panel;
  private JLabel messageLabel;
  private JTextField sleepTextField;
  private JTextField sleepAnswerField;
  private final int WINDOW_WIDTH = 310;
  private final int WINDOW_HEIGHT = 100;
  public SleepCounter() {
    setTitle("Sleep Counter");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);
  }
  private void buildPanel() {
    dailyLabel = new JLabel("Enter Sleep " + "in hours");
    hourTextField = new JTextField(10);
    CalcButton = new JButton("Calculate");
    CalcButton.addActionListener(new CalcButtonListener());
    panel = new JPanel();
    panel.add(dailyLabel);
    panel.add(sleepTextField);
    panel.add(CalcButton);
    panel.add(sleepAnswerField);
  }
  private class CalcButtonListener implements ActionListener {
    public void actionPerformer(ActionEvent e) {
      String input;
      int total;
      input = sleepTextField.getText();
      for (int i = 0; i < 7; i++); {
        input += total;
      }
      JOptionPane.showMessageDialog(null, "The total amount of sleep for " + (i + 1) + "days is" + total);
      if (int i > 7) {
        double avg = (total / 7);
        JOptionPane.showMessageDialog(null, "The avg amount of sleep for 7 days is" + avg);
      }
    }
  }
}

When running I get an error message saying:

Error: Could not find or laod main class Graduation Project.

I've done a search of this site and checked the answers as much as I know how. I would appreciate any help that you offer.

1
  • Are you using a IDE (netbeans, eclipse, jcreator) to code this class? Commented Apr 8, 2013 at 22:51

2 Answers 2

1

You have put main method outside the class

 public class  YourClass
 {
  public static void main(String args[])
   {  
   }
  }

place it inside and remember you cant put anything before these imports

import javax.swing.*;
import java.awt.event.*;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you to everyone. I'll work in these changes and hopefully that will fix the problem. And I really appreciate the help. Hopefully I'll be experienced enough in the future to help someone else.
0

It's a bit hard to tell from your formatting. But if you're going to be running a class called "Graduation", you're code is going to have to look like this:

public class Graduation{ 


  public static void main (String[] args){
  }

}

You need the Graduation class, and it needs to have a main method that conforms to that. It's not clear from your example that your methods are actually inside a class. Is it compiling?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.