Reputation: 19
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)
What is wrong?
Upvotes: 0
Views: 760
Reputation: 528
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.
Upvotes: 1
Reputation: 61526
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
Upvotes: 3
Reputation: 104168
Just some suggestions:
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;
}
}
Upvotes: 2