PDA

View Full Version : need help with a project


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

KeZZeR
04-30-2007, 04:14 PM
Can nested classes be public in Java?

Aradon
04-30-2007, 04:31 PM
No, a class within a method cannot be public, it has to be final or static (I think static).

However a class within a class can. Right now in your code you have the ButtonHandler class inside of your AddressBook constructor. This wont work in the way you think it will. Doing this will not allow you to use ButtonHandler like you did here:


ButtonHandler handler = new ButtonHandler();


What you need to do is move it Outside of your constructor but still inside the AddressBook Class.

Here is your code with it moved to the right spot:



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



You still have a few more errors to get through before your error free, but you're close!

Lockwood
05-01-2007, 12:59 AM
thanks for that...but what does this error mean?

J:\Java\AddressBook.java:68: actionPerformed(java.awt.event.ActionEvent) in AddressBook.ButtonHandler cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; overridden method does not throw java.io.FileNotFoundException
public void actionPerformed(ActionEvent event)throws FileNotFoundException
^
1 error

Tool completed with exit code 1

Aradon
05-01-2007, 03:39 AM
thanks for that...but what does this error mean?

J:\Java\AddressBook.java:68: actionPerformed(java.awt.event.ActionEvent) in AddressBook.ButtonHandler cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; overridden method does not throw java.io.FileNotFoundException
public void actionPerformed(ActionEvent event)throws FileNotFoundException
^
1 error

Tool completed with exit code 1

I believe it's saying that your code wont throw a FileNotFoundException at all. That is because you are catching it before it can be thrown with the try catch.



try
{
PrintWriter out = new PrintWriter(new FileWriter("Names.doc"));
if (out == null)throw new FileNotFoundException();
Names = (sc.next() + " " + sc.next());
}
catch(FileNotFoundException e)
{
}



Also, another exception you may be missing is an IOException of the general type. But that is purely speculation :P

As such though, you don't have to say that the method is throwing it:


public void actionPerformed(ActionEvent event)throws FileNotFoundException // You're not throwing that!

Wolvy1
05-02-2007, 12:15 PM
No, a class within a method cannot be public, it has to be final or static (I think static).

A method local inner class, can not be static
A static nested class, is not even an inner class, but rather a top level nested class, with no access to any of the instance members. It must be placed outside of any method body.

A method local inner class can not use any access modifiers, and can only be marked as either final or abstract (but does not require either modifier).