Lockwood
04-30-2007, 03:48 AM
I'm currently in intro to programming and we got a project due on Friday. We're making an address book with Java. My partner and I are trying to figure out how to send info typed into a JTextArea to an external file, then be able to read in what we sent from that file (by clicking another button). Also, we are going to add a search button that searches for the name, and brings up the previously added info about that person. Any help as to what we should do next with our program would be appreciated. Here's what we got so far:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class AddressBook extends JFrame
{
private JButton AddButton, SearchButton;
public String Names = "";
public Scanner sc, sc2;
public Container Interface;
public AddressBook()throws FileNotFoundException
{
//Enter title and create the interface
super ("Bowswood's Java Easy Address Book");
Interface = getContentPane();
Interface.setLayout(new FlowLayout());
//Create a label with directions
Label Infolbl = new Label("Enter info starting with your name");
//create text area in order to enter info
JTextArea TArea = new JTextArea();
String Info = TArea.getText();
TArea.setEditable(false);
TArea.setRows(20);
TArea.setColumns(50);
getContentPane().add(new JScrollPane(TArea), BorderLayout.CENTER);
pack();
AddButton = new JButton("Add Info");
Interface.add(AddButton);
SearchButton = new JButton("Search");
Interface.add(SearchButton);
//Create Variables
ButtonHandler handler = new ButtonHandler();
AddButton.addActionListener(handler);
SearchButton.addActionListener( handler );
setSize(300, 150);
setVisible(true);
int count = 0;
sc = new Scanner(Info);
sc2 = new Scanner(new File("Names.doc"));
while(sc2.hasNext())
{
String temp = sc2.nextLine();
count++;
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)throws FileNotFoundException
{
try
{
PrintWriter out = new PrintWriter(new FileWriter("Names.doc"));
if (out == null)throw new FileNotFoundException();
Names = (sc.next() + " " + sc.next());
}
catch(FileNotFoundException e)
{
}
}
}
}
}
}
public static void main(String[] args)throws FileNotFoundException
{
AddressBook application = new AddressBook();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Errors:
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:68: illegal start of expression
public class ButtonHandler implements ActionListener
^
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:68: ';' expected
public class ButtonHandler implements ActionListener
^
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:87: 'class' or 'interface' expected
}
^
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:95: 'class' or 'interface' expected
^
4 errors
Tool completed with exit code 1
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class AddressBook extends JFrame
{
private JButton AddButton, SearchButton;
public String Names = "";
public Scanner sc, sc2;
public Container Interface;
public AddressBook()throws FileNotFoundException
{
//Enter title and create the interface
super ("Bowswood's Java Easy Address Book");
Interface = getContentPane();
Interface.setLayout(new FlowLayout());
//Create a label with directions
Label Infolbl = new Label("Enter info starting with your name");
//create text area in order to enter info
JTextArea TArea = new JTextArea();
String Info = TArea.getText();
TArea.setEditable(false);
TArea.setRows(20);
TArea.setColumns(50);
getContentPane().add(new JScrollPane(TArea), BorderLayout.CENTER);
pack();
AddButton = new JButton("Add Info");
Interface.add(AddButton);
SearchButton = new JButton("Search");
Interface.add(SearchButton);
//Create Variables
ButtonHandler handler = new ButtonHandler();
AddButton.addActionListener(handler);
SearchButton.addActionListener( handler );
setSize(300, 150);
setVisible(true);
int count = 0;
sc = new Scanner(Info);
sc2 = new Scanner(new File("Names.doc"));
while(sc2.hasNext())
{
String temp = sc2.nextLine();
count++;
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)throws FileNotFoundException
{
try
{
PrintWriter out = new PrintWriter(new FileWriter("Names.doc"));
if (out == null)throw new FileNotFoundException();
Names = (sc.next() + " " + sc.next());
}
catch(FileNotFoundException e)
{
}
}
}
}
}
}
public static void main(String[] args)throws FileNotFoundException
{
AddressBook application = new AddressBook();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Errors:
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:68: illegal start of expression
public class ButtonHandler implements ActionListener
^
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:68: ';' expected
public class ButtonHandler implements ActionListener
^
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:87: 'class' or 'interface' expected
}
^
H:\My Documents\Java dayton is a square\Final project\AddressBook.java:95: 'class' or 'interface' expected
^
4 errors
Tool completed with exit code 1