tags:

views:

246

answers:

6

I can't get my program to compile!

i think im missing a curly brace but can't for the life of me see where!

        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;
        import java.io.*;
        import java.lang.*;
        import java.text.*;
        import java.net.*;
        import java.util.Scanner;

    public class AddressBook extends JFrame 
    {

FlowLayout leftLayout;

    JFrame frame;
    JPanel panel;
    JTextField txtname,txtsurname, txtphone, txtmobile, txtaddress, txtpostcode;
    JButton btnadd, btnnext, btnprevious, btnsave, btndelete;
    JLabel jlbname, jlbsurname, jlbphone, jlbmobile, jlbaddress, jlbpostcode;


    String fileInput,readline;
    ArrayList<String> arrayOfFile = new ArrayList<String>();
    ArrayList<Contact> records = new ArrayList<Contact>();



    int index = 0;


    public static void main(String[] args) throws IOException
    {
    new AddressBook();
    }

    public AddressBook()
    {

    //sets window
    frame = new JFrame();
    frame.setTitle("Bournemouth University Address Book");
    frame.setSize(760, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //sets up panel
    panel = new JPanel();
    panel.setLayout(null);
    frame.getContentPane().add(panel);



  //Labels
    jlbname = new JLabel("Name:");
    jlbname.setBounds(10, 50, 100, 20);
    panel.add(jlbname);

    jlbsurname = new JLabel("Surname:");
    jlbsurname.setBounds(350, 50, 100, 20);
    panel.add(jlbsurname);

    jlbphone = new JLabel("Home Number:");
    jlbphone.setBounds(10, 90, 150, 20);
    panel.add(jlbphone);

    jlbmobile = new JLabel("Mobile:");
    jlbmobile.setBounds(350, 90, 150, 20);
    panel.add(jlbmobile);

    jlbaddress = new JLabel("Address:");
    jlbaddress.setBounds(10, 130, 200, 20);
    panel.add(jlbaddress);

    jlbpostcode = new JLabel("PostCode:");
    jlbpostcode.setBounds(10, 170, 250, 20);
    panel.add(jlbpostcode);

    //Text Fields
    txtname = new JTextField("");
    txtname.setBounds(120, 50, 200, 20);
    panel.add(txtname);

    txtsurname = new JTextField("");
    txtsurname.setBounds(440, 50, 200, 20);
    panel.add(txtsurname);

    txtphone = new JTextField("");
    txtphone.setBounds(120, 90, 200, 20);
    panel.add(txtphone);

    txtmobile = new JTextField("");
    txtmobile.setBounds(440, 90, 200, 20);
    panel.add(txtmobile);

    txtaddress = new JTextField("");
    txtaddress.setBounds(120, 130, 520, 20);
    panel.add(txtaddress);

    txtpostcode = new JTextField("");
    txtpostcode.setBounds(120, 170, 250, 20);
    panel.add(txtpostcode);




    //Buttons
    btnadd = new JButton("Add", new ImageIcon("../files/add.png"));
    btnadd.setBounds(330, 320, 100, 50);
    btnadd.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
    btnadd.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            txtname.setText("Add new details here");
   txtsurname.setText("");
   txtphone.setText("");
   txtmobile.setText("");
   txtaddress.setText("");
   txtpostcode.setText("");   
                        }
                    });
    panel.add(btnadd);

    btndelete = new JButton("Delete", new ImageIcon("../files/delete2.png"));
    btndelete.setBounds(390, 250, 100, 50);
    btndelete.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btndelete.setForeground(Color.red);
      //        btndelete.addActionListener(this);
    panel.add(btndelete);

    btnsave = new JButton("Save", new ImageIcon("../files/save.png"));
    btnsave.setBounds(490, 250, 100, 50);
    btnsave.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
    btnsave.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                          try
  {
  BufferedWriter fileOut = new BufferedWriter(new FileWriter("../files/contacts.buab", true)); 
  fileOut.append(txtname.getText());
  fileOut.append("\n");    
  fileOut.append(txtsurname.getText());
  fileOut.append("\n");  
  fileOut.append(txtphone.getText());
  fileOut.append("\n");     
  fileOut.append(txtmobile.getText());
  fileOut.append("\n");  
  fileOut.append(txtaddress.getText());
  fileOut.append("\n");   
  fileOut.append(txtpostcode.getText() + "\r");



  fileOut.close(); 
     }
  catch (IOException ioe)
  {
  JOptionPane.showMessageDialog(null, ioe.getMessage());
                }

                        }
                    });
    panel.add(btnsave);

    btnprevious = new JButton("Prev", new ImageIcon("../files/left.png"));
    btnprevious.setBounds(280, 250, 100, 50);
    btnprevious.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
    btnprevious.addActionListener(new ActionListener()
                    {public void actionPerformed(ActionEvent event)
                        {
                            index--;
                        displaycontact();
                        }

                    });
    panel.add(btnprevious);

    btnnext = new JButton("Next", new ImageIcon("../files/right.png"));
    btnnext.setBounds(180, 250, 100, 50);
    btnnext.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
    btnnext.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                         index ++;
                         displaycontact();
                         }


                    });
    panel.add(btnnext);

    frame.setVisible(true);
    panel.setVisible(true);




 JMenuBar mb = new JMenuBar();
            frame.setJMenuBar(mb);

            JMenu insert = new JMenu("Import");
            mb.add(insert);
            JMenuItem imp = new JMenuItem("Add New Contacts");
            insert.add(imp);
            imp.addActionListener(new ActionListener()

          {public void actionPerformed(ActionEvent event)
    {
     JFileChooser fileopen = new JFileChooser();

        int ret = fileopen.showDialog(null, "Open file");

        if (ret == JFileChooser.APPROVE_OPTION) 
        {
          try {
            BufferedReader fileStream = new BufferedReader(new FileReader("src/contacts.buab"));
            while (true)
            {
            String fileInput = fileStream.readLine();
            if(fileInput==null)
                break;

     Contact a = new Contact();
            a.setname(fileInput);
            a.setsurname(fileStream.readline());
            a.setphone(fileStream.readLine());
            a.setmobile(fileStream.readLine());
            a.setaddress(fileStream.readLine());
            a.setpostcode(fileStream.readline());
            Contacts.add(a);
     System.out.println(a.getname());

            }

            fileStream.close();
                   }
                   catch (Exception ex) 
                     {  
                     JOptionPane.showMessageDialog(null, ioe.getMessage());
                }
            displaycontact();
                   }});






        }

          public void displaycontact()
         {
             txtname.setText(contacts.get(index).name);
             txtsurname.SetText(contacts.get(index).surname);
             txtphone.setText(contacts.get(index).phone);
             txtmobile.setText(contacts.get(index).mobile);
             txtAddress.setText(contacts.get(index).address);
         }


    }


  }

please help i've been here for 3 hours!!

+2  A: 
  1. new should be all lower-case
  2. ioe should be ex
  3. Remove the bottom-most curly-bracket
  4. Add one more closing curly bracket after JOptionPane.showMessageDialog(null, ioe.getMessage()); displaycontact();
  5. Use the prescribed coding conventions, especially the indentation ones
Bozho
+1  A: 

You appear to have a spurious open curly brace in front of public void actionPerformed.

But really, you need to be able to sort these out yourself. You can't post your code to SO every time it doesn't compile... Something that may help here is an editor that can do folding (just about every IDE would do this), or even the functionality that vi has when you press % on a brace/bracket/quote etc. (which jumps to the closing symbol letting you match up start and end).

Plus, whenever you ask a question, always provide the diagnostic information - which in this case would be the compiler output. It's basically rude to expect people to help you while withholding pertinent information.

Andrzej Doyle
i've been here for 3 hours! its not like it just happened
addiosamigo
Andrzej Doyle
@dtsazza: I second your point about sorting out these errors yourself. However I wanted to point out that the curly brace in front of `public void actionPerformed` is needed because that is a function declaration. addiosamigo was actually missing the closing brace for that opening one.
Mr. Shiny and New
Good point, Mr. Shiny. That's yet another reason for proper indentation, since that brace looked very much like a typo where it was/is.
Andrzej Doyle
+1  A: 

You're short one closing curly brace after the last displaycontact();

You then need to remove the final curly brace.

dpwood
+1  A: 

addiosamigo, I would really recommend that you be able to deal with these kinds of errors youself, since it will help you much more in the future.

To help in that, here are the steps that I would take to solve this kind of thing:

  1. Delete all the code from the file, except the minimum necessary to compile. See if this compiles (if not, you have a serious problem that's not part of the code. Perhaps a problem with a project definition or something).
  2. Start adding code back in very small chunks.
  3. Each time you add code back, make sure it compiles.
  4. Eventually, you'll add something that doesn't compile, and you'll know the bad code is there.

Please note that doing this is not always so simple, since sometimes you need to add code in a certain order for it to compile.

For example, adding the code for function Foo, which calls function Bar, without adding the code for Bar will obviously cause a compile error. In this kind of case, you'll have to do smart tricks (like adding Bar back in, without any actual implementation, etc).

All in all, the strategy is the same as for every bug: find the smallest case that reproduces it, and work from there.

Hope this helps

Edan Maor
thanks for that, i'll do that if i get problems again
addiosamigo
Glad I could help.
Edan Maor
+6  A: 

You are missing a curly brace here:

            displaycontact();
        }}});  // <- HERE

This is nearly impossible to see with your code because it is formatted very poorly. You should use a text editor which highlights matching braces. This lets you see quickly what that closing brace closes.

I advise you to reformat the code so that there is proper indentation. You have quite a lot of indentation so you may want to consider using two spaces or a tab size with the tab width set to two. Proper indentation lets you scan the code veritcally to see where the braces are closing things.

I reformatted your code in Eclipse and the ActionListener which is causing your problem now looks like this:

imp.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent event) {
    JFileChooser fileopen = new JFileChooser();

    int ret = fileopen.showDialog(null, "Open file");

    if (ret == JFileChooser.APPROVE_OPTION) {
      try {
        BufferedReader fileStream = new BufferedReader(new FileReader("src/contacts.buab"));
        while (true) {
          String fileInput = fileStream.readLine();
          if (fileInput == null)
            break;

          Contact a = new Contact();
          a.setname(fileInput);
          a.setsurname(fileStream.readline());
          a.setphone(fileStream.readLine());
          a.setmobile(fileStream.readLine());
          a.setaddress(fileStream.readLine());
          a.setpostcode(fileStream.readline());
          Contacts.add(a);
          System.out.println(a.getname());

        }

        fileStream.close();
      } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ioe.getMessage());
      }
      displaycontact();
    }
  }
});

Notice that the last curly brace blob is now in three lines and that each line is less indented than the previous? This would make it immediately obvious where you were missing a curly brace.

Mr. Shiny and New
thank you! thank you thank you!
addiosamigo
Good job, going through that code was a mess.
C. Ross
SO the best IDE!!!
Carlos Heuberger
The beauty of using Eclipse (or any decent IDE) is that I didn't have to read the code to find the compilation error. Paste it in, go to the line where the compiler indicates the error, and you can see right away that the brackets aren't lining up because of the matching bracket highlight. Add one brace where indicated, remove the last brace (I guess it was the misplaced one?) and CTRL-SHIFT-F, and you're done. IDEs add so much productivity.
Mr. Shiny and New
A: 

The if statement

if (ret == JFileChooser.APPROVE_OPTION)

is missing it's closing brace, you must add it just after

displaycontact();

at the end of the source is an extra brace.

A few hints to help yourself in the future:

  • make sure your code indenting is correct, fixing your whitespacing and indenting would have lead you to the culprit
  • anonymous classes can make code unreadable if they consist of more than a few lines, your action listener is a fine example of this. code becomes much more readable if you create a separate nested class for it instead of inserting large anonymous classes.

Edit: to get started have a look at Sun's Java code conventions like and its indenting chapter. IDE's like eclipse supoort formatting and indenting of your code too.

rsp
is there any good sites that help you with indentation? i've never really learnt it.
addiosamigo