简体   繁体   English

Java Arraylist-编译错误

[英]Java Arraylist - Compilation Error

I'm trying to get my program to compile but its giving me 5 errors 我正在尝试让我的程序编译,但是它给了我5条错误

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)

What is wrong? 怎么了?

First off this is Java so please follow the naming conventions and good programming practices: 首先是Java,因此请遵循命名约定和良好的编程习惯:

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: Next Java是一种OO语言,因此让我们使用这些功能:

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: 我宁愿使用Scanner而不是BufferedReader,因为它更加灵活,因此我将构造函数更改为:

    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. 即使您不采用Person类的想法,也要采用显示的想法,这样您就不会在3个地方使用相同的代码...这将使您的生活更加轻松。

The only issue now is that you never posted what the error was, so I cannot really help you with that part (yet). 现在唯一的问题是您从未发布过错误信息,所以我还无法真正帮助您解决这一部分。

Blockquote 块引用

Just some suggestions: 只是一些建议:

  • ArrayList names shouldn't start with a capital letter (name instead of Name). ArrayList名称不应以大写字母开头(名称而不是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. ArrayLists应该是您的类的私有成员。 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. 为避免使用许多ArrayList,应定义一个Person类,该类具有名称,姓氏,电话作为成员。

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;
  }
}

i think it has to do with the nested if in: 我认为这与嵌套if有关:

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. 否则,一旦b变为0,它将转到下一个if语句。

and check what kgiannakakis said... it is very important. 并检查kiannakakis所说的话...这很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM