Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: class, interface, or enum expected errors??

  1. #1
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question class, interface, or enum expected errors??

    The following code is supposed to create a small gui that will allow for text to be read from, and written to an external file. I believe that the code is correct, at least as best as I know, but when I try to compile it I get errors complaining about the main class that I do not understand....
    I will post the code I have here, and hope for some advice on this.
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    public class Newclass2 extends JFrame implements ActionListener
    {
      private String s1, s2;
      static String s3;
      private JFrame f = new JFrame("text testing");
      private JTextField txt = new JTextField(24);
      private JTextField txt2 = new JTextField(24);
      private JButton b1 = new JButton("Submit");
      private JButton b2 = new JButton("Exit");
     
        public Newclass2(){
           f.setLayout(new BorderLayout());
          JPanel p = new JPanel();
           p.setLayout(new FlowLayout());
           p.add(new JLabel("text1"));
           p.add(txt);
           p.add(new JLabel("text2"));
           p.add(txt2);
           f.add(p, BorderLayout.CENTER);
           JPanel p2 = new JPanel();
           p2.setLayout(new FlowLayout());
           p2.add(b1);
           p2.add(b2);
           add.addActionListener(this);
           open.addActionListener(this);
            f.add(p2, BorderLayout.SOUTH);
            f.setVisible(true);
            f.setSize(480,480);
    }    
        @Override
        public void actionPerformed(ActionEvent e){
        if(e.getSource()==b1){
            String txtemp1 = "";
            String txttemp2 = "";
            String txttemp1 = txt.getText().trim();
            txttemp2 = txt2.getText().trim();
            String txttemps2 = null;
             s3 =("text here"+txttemp1) + (txttemps2);
            String data = Newclass2.s3;
        try{    try (BufferedWriter readz = BufferedWriter(new FileWriter(new File
            ("C:/users/zlloyd1/desktop/test1.doc")))) {
                readz.write(data);
                readz.newLine();
            }
             System.out.println("Done");
                }catch(Exception e2){System.out.println("Error message text");}
               }
           if(e.getSource()==b2){
             JFileChooser choosa = new JFileChooser("C:/users/zlloyd1/desktop/test1.doc");
              int x = choosa.showOpenDialog(null);
              if(x == JFileChooser.APPROVE_OPTION){
              File file = choosa.getSelectedFile();}}}}
    //try(
    //Desktop.getDesktop().open(file);}catch(Exception e3){}}}}
            private static void main(String[] args){
                 new Newclass2();
           }
       }
    }
    I commented out two lines of code near the end of the program that were giving me a world of trouble, but it still will not recognize my main class.
    This is the errors that the compiler spits at me:

    newclass2.java:64: error: class, interface, or enum expected
    private static void main(String[] args){
    ^
    newclass2.java:66: error: class, interface, or enum expected
    }}
    ^

    It also seems to have a problem with add, open, and BufferedWriter, when I try to load this code into NetBeans, but I thought those were built in Java methods. It seems that is NOT the case, because it is telling me "cannot find symbol" for all three of them and suggesting that I create a method for them.
    here is the lines where this is happening:
    try{    try (BufferedWriter readz = BufferedWriter(new FileWriter(new File
            ("C:/users/zlloyd1/desktop/test1.doc")))) {
    for the BufferedWriter, but it is strange, because it does not have an issue with the first one, just the second BufferedWriter??
    and for the add, and open it is these two lines that are a problem:
    add.addActionListener(this);
    open.addActionListener(this);
    I am not sure if I have misused this here for these, but it is not recognizing add, nor open as legitimate commands, or methods, even though I have all the necessary imported libraries, I believe????
    Can someone PLEASE tell me what I am doing wrong here, in Java for Dummies language??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: class, interface, or enum expected errors??

    class, interface, or enum expected
    That error often means there are mis matched {}s. Check that all { have a matching }
    and that all methods are not contained inside the {}s of other methods or statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: class, interface, or enum expected errors??

    Your code should never look like this:

    File file = choosa.getSelectedFile();}}}}
    //try(
    //Desktop.getDesktop().open(file);}catch(Exception e3){}}}}

    You should be careful with your indentation and put the end curly brace, }, on its own line, precisely indented to match the opening brace line so you can more easily match them. Use white space to your advantage.

  4. #4
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: class, interface, or enum expected errors??

    --- Update ---

    --- Update ---

    Quote Originally Posted by curmudgeon View Post
    You should be careful with your indentation and put the end curly brace, }, on its own line, precisely indented to match the opening brace line so you can more easily match them. Use white space to your advantage.
    Thanks for the advice, but I do not usually concern myself with indentation until after I have the program working, and I have not gotten there as yet!!

    --- Update ---

    Quote Originally Posted by Norm View Post
    That error often means there are mis matched {}s. Check that all { have a matching }
    and that all methods are not contained inside the {}s of other methods or statements.
    Thank you Norm, for a simple and helpful answer!!
    I will go through and be sure that all opened brackets and parenthesis are closed!!

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: class, interface, or enum expected errors??

    Quote Originally Posted by zlloyd1 View Post
    Thanks for the advice, but I do not usually concern myself with indentation until after I have the program working, and I have not gotten there as yet!! :"
    Does you IDE not auto-format correctly until the program is error free? (Eclipse LOL)

  6. #6
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Red face Re: class, interface, or enum expected errors??

    I am not using an IDE at this time, because I am still trying to learn, and have been told that an IDE can be confusing if you do not have the language down completely. I am doing my coding in Notepad++, and compiling and running in the command prompt, so indentation can be a chore....

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: class, interface, or enum expected errors??

    Quote Originally Posted by zlloyd1 View Post
    I am not using an IDE at this time, because I am still trying to learn, and have been told that an IDE can be confusing if you do not have the language down completely. I am doing my coding in Notepad++, and compiling and running in the command prompt, so indentation can be a chore....
    I understand, but it is one chore that is going to help you complete your program, so it's a habit well worth getting into.

Similar Threads

  1. Error: class, interface, or enum expected.
    By duderski in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2012, 01:26 PM
  2. Class, Interface, or Enum expected
    By SilvioSpeed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2012, 09:34 AM
  3. class,interface, or enum expected?
    By Eimaj in forum What's Wrong With My Code?
    Replies: 21
    Last Post: September 28th, 2012, 03:16 PM
  4. [SOLVED] Error: Class,interface or enum expected
    By FJIW in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 18th, 2011, 03:08 PM
  5. Error of "class or interface expected" at Mylong class for problem of API's
    By lostgoat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 22nd, 2009, 08:28 PM

Tags for this Thread