tags:

views:

146

answers:

3

hi,

i'm trying to get my program to compile but its giving me 5 errors

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 implements ActionListener
{

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;

    List<Person> people = new ArrayList<Person>();




    int i = 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 InnerAListener());
             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(new InnerBListener());
                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 InnerCListener());
                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 InnerDListener());
             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 InnerEListener());
             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 InnerFListener());

            }




            private class InnerAListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Clearscreen();
                                            }
                       }

                       private class InnerBListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Delete();
                                            }
                       }

                       private class InnerCListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)

                                            {
                                            Save();
                                            }
                       }



                       private class InnerDListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Previous();
                                            }
                       }

                       private class InnerEListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Next();
                                            }
                       }

                       private class InnerFListener
                       implements ActionListener
                       {
                        public void actionPerformed(final ActionEvent e)

                          {
                          ImportContacts();
                          }
                       }




  public void previous()
  {
       if (i > 0)
       {
           i--;
       }

       display(people.get(i));    
  }

  public void Next()
  {
       if(i < people.size() - 1)
                 {
           i++;
       }

       display(people.get(i));
  }

  private void display(final Person person)
  {
       txtname.setText(person.getname());
       txtsurname.setText(person.getsurname());
       txtphone.setText(person.getphone());
       txtmobile.setText(person.getmobile());
       txtaddress.setText(person.getaddress());
       txtpostcode.setText(person.getpostcode());
  }

  public void Save()
  {
    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());
                    }

                }


  public void Clearscreen()

   {
   txtname.setText("Add new details here");
   txtsurname.setText("");
   txtphone.setText("");
   txtmobile.setText("");
   txtaddress.setText("");
   txtpostcode.setText("");
   }

  public void ImportContacts
  {

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

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

         if (ret == JFileChooser.APPROVE_OPTION) 

         {
                 try
   {
        final File    file;
        final Scanner scanner;

        file    = new File("../files/contacts.buab");
        scanner = new Scanner(file);

        while(scanner.hasNextLine())
       {
          final Person person;

          person = new Person(scanner);
          people.add(person);
       }
    }
    catch (IOException ioe)
    {
        JOptionPane.showMessageDialog(null, ioe.getMessage());
    }

    display(people.get(0)); 

        }

     }     
    };

}

the errors all start:

    public void ImportContacts
    {

they say the { should be a (, then it says illegal start of expression, the go onto the line below

     public void actionPerformed(ActionEvent event)

i can't see whats wrong?

+1  A: 

Just some suggestions:

  • ArrayList names shouldn't start with a capital letter (name instead of Name). Only classes should start with a capital letter. This is a style convention that will make your code more readable and not a strict rule.
  • ArrayLists should be private members of your class. This is not evident from the code you've posted.
  • To avoid using many ArrayLists you should define a Person class, that has name, surname, phone as members.

Something like this will do:

public class Person {
  private String name;
  private String surname;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getSurname() {
    return surname;
  }  
  public void setSurname(String name) {
    this.surname = surname;
  }
}
kgiannakakis
+1  A: 

i think it has to do with the nested if in:

for (a = 0, b = 0; a < temp.size(); a++, b++) {
  if (b == 5) {
    b = 0;
  }
  if (b == 0)
// ...

you have to change it to:

if(b == 5) {
  b = 0;
} else if(b == 0) {
     //...
} else if ...

otherwise, once b turns 0, then it will go to the next if statement.

and check what kgiannakakis said... it is very important.

Oso
+3  A: 

First off this is Java so please follow the naming conventions and good programming practices:

List<String> name = new ArrayList<String>();
List<String> surname = new ArrayList<String>();
List<String> phone = new ArrayList<String>();
List<String> mobile = new ArrayList<String>();
List<String> address = new ArrayList<String>();
List<String> temp = new ArrayList<String>();

try
{
    BufferedReader infoReader = new BufferedReader(new FileReader("../files/contacts.buab"));
    int i = 0;
    String loadContacts;

    while ((loadContacts = infoReader.readLine()) !=null)
    {
        temp.add(loadContacts);
        i++;
    }

    int a = 0;
    int b = 0;

    for (a = 0, b = 0; a < temp.size(); a++, b++)
    {
        if (b == 0)
        {
            name.add(temp.get(a));    
        }
        else if (b == 1)
        {
            surname.add(temp.get(a));    
        }
        else if (b == 2)
        {
            phone.add(temp.get(a));    
        }
        else if (b == 3)
        {
            mobile.add(temp.get(a));    
        }   
        else if (b == 4)
        {
            address.add(temp.get(a));    
        }         
        else if (b == 5)
        {
            b = 0;
        }
    }
}
catch (IOException ioe)
{
    JOptionPane.showMessageDialog(null, ioe.getMessage());
}

txtName.setText(name.get(0));
txtSurname.setText(surname.get(0));
txtPhone.setText(phone.get(0));
txtMobile.setText(mobile.get(0));
txtAddress.setText(address.get(0));

public void previous()
{
    if (i > 0)
    {
        i--;
    }

    txtName.setText(name.get(i));
    txtSurname.setText(surname.get(i));
    txtPhone.setText(phone.get(i));
    txtMobile.setText(mobile.get(i));
    txtAddress.setText(address.get(i));
}

public void Next()
{
    if(i < temp.size() - 1)
    {
        i++;
    }

    txtName.setText(name.get(i));
    txtSurname.setText(surname.get(i));
    txtPhone.setText(phone.get(i));
    txtMobile.setText(mobile.get(i));
    txtAddress.setText(address.get(i));
 }

Next Java is an OO language, so lets use those features:

public class Person
{
    private final String firstName;
    private final String lastName;
    private final String phone;
    private final String mobile;
    private final String address;

    public Person(final BufferedReader reader)
        throws IOException,
               InvalidPersonException
    {
        firstName = readLine(reader, "First name");
        lastName  = readLine(reader, "Last name");
        phone     = readLine(reader, "Phone");
        mobile    = readLine(reader, "Mobile");
        address   = readLine(reader, "Address");
    }

    private static String readLine(final BufferedReader reader, 
                                   final String type)
        throws IOException,
               InvalidPersonException
    {
        final String line;

        line = reader.readLine();

        if(line == null)
        {
            throw new InvalidPersonException("missing: " + type);
        }

        return (line);
    }

    public String getFirstName()
    {
        return (firstName);
    }

    public String getLastName()
    {
        return (lastName);
    }

    public String getPhone()
    {
        return (phone);
    }

    public String getMobile()
    {
        return (mobile);
    }

    public String getAddress()
    {
        return (address);
    }
}

I'd rather use the Scanner instead of a BufferedReader since it is more flexible, so I would change the constructor to:

    public Person(final Scanner scanner)
        throws IOException,
               InvalidPersonException
    {
        firstName = readLine(reader, "First name");
        lastName  = readLine(reader, "Last name");
        phone     = readLine(reader, "Phone");
        mobile    = readLine(reader, "Mobile");
        address   = readLine(reader, "Address");
    }

    private static String readLine(final BufferedReader reader, 
                                   final String type)
        throws IOException,
               InvalidPersonException
    {
        final String line;

        if(!(scanner.hasNextLine()))
        {
            throw new InvalidPersonException("missing: " + type);
        }

        line = scanner.nextLine();

        return (line);
    }

That will then simplify the code to (along with some other changes):

List<Person> people = new ArrayList<Person>();

try
{
    final File    file;
    final Scanner scanner;

    file    = new File("../files/contacts.buab");
    scanner = new Scanner(file);

    while(scanner.hasNextLine())
    {
        final Person person;

        person = new Person(scanner);
        people.add(person);
    }
}
catch (IOException ioe)
{
    JOptionPane.showMessageDialog(null, ioe.getMessage());
}

display(people.get(0); 

public void previous()
{
    if (i > 0)
    {
        i--;
    }

    display(people.get(i));    
}

public void Next()
{
    if(i < people.size() - 1)
    {
        i++;
    }

    display(people.get(i));
 }

 private void display(final Person person)
 {
     txtName.setText(person.getFirstName());
     txtSurname.setText(person.getLastName());
     txtPhone.setText(person.getPhone());
     txtMobile.setText(person.getMobile());
     txtAddress.setText(person.getAddress());
 }

Even if you do not take the Person class idea take the display idea so that you do not have the same code in 3 places... it'll make your life easier.

The only issue now is that you never posted what the error was, so I cannot really help you with that part (yet).

Blockquote

TofuBeer
thanks man, that looks much more neater
addiosamigo
thanks for all your help! i'm very new to java. the first bit of code does that have to be in a seperate class, i.e seperate file??where does the other bits go?
addiosamigo
I would put each non-inner class in its own file. So I would make a Person.java file and put it in the same location as the other. For the rest of it just put the code in the class you already have it in.
TofuBeer
can you check it for me its giving me 38 errors??http://pastebin.com/d7fb5279fthanks for all you help! its driving me mad!
addiosamigo
well how about you update your question with your current code (all of it) and either I or someone else can help you (I am teaching a class in Java programming as we type... so I have to help them before you :-)
TofuBeer
ok mate, i posted it on pastebin, but i'll post it on here to!whats your paypal address, i'll send you a gratitude gift for all your help!
addiosamigo
nope... I teach 'cause I like to :-)
TofuBeer
and that being said, since this is homework (!) you need to make sure you understand what it is doing and take the lessons from this and apply them to other assignments.
TofuBeer
i find i'm learning when its working, I read through it an understand it better this way. if i was to just sit down and write a program, i find i don't understand what its doing.going back to my program i think i may have one of the codes in the wrong place, can't see where tho, i'm working on it!
addiosamigo
the current problem you have is that you have put a couple of methods inside another method (in your actionPerformed). The problem is really that you are trying to jam too much code into the method to begin with - the comment I made in your post will help you simplify the code for that method and make it easier to spot where you are going wrong.
TofuBeer
I'm not sure if what i'm doing is right i'm using the 2nd example i posted
addiosamigo
i'm really stuck now i have 96 errors!
addiosamigo
almost done for the day - will check when I get home :-)
TofuBeer
thanks man, i think i'm doing the innerlistner wrong!
addiosamigo