Problem with string tokenizer
I have the required information saved to "patients.txt" but when i enter the name i want to "search" by the rest of the information should be read from the file and printed to the relevant text fields... i keep getting the exception message ie "Cant open file"... im thinking this is because i have not opened the file correctly?...
ps please ignore the crappy gui
Code:
import java.awt.*;
import java.awt.event.*;
import javax .swing.*;
import java.io.*;
import java.util.StringTokenizer;
public class FindPatient extends JFrame implements ActionListener
{
private JLabel Name,MRN,Address,Age,Admission,Wardname,Wardnumber;
private JTextField name,mrn,address,age,admission,wardname,wardnumber;
private JButton Search;
public static void main(String args[])
{
FindPatient app = new FindPatient();
}
public FindPatient() //constructor
{
super("Event Handling");
Container c = getContentPane();
c.setLayout(new FlowLayout());
Name = new JLabel("Patient Name");
c.add(Name);
name = new JTextField(20);
c.add(name);
Search = new JButton("Search");
Search.addActionListener(this);
c.add(Search);
MRN = new JLabel("MRN");
c.add(MRN);
mrn = new JTextField(20);
c.add(mrn);
Address = new JLabel("Address ");
c.add(Address);
address = new JTextField(20);
c.add(address);
Age = new JLabel("Age ");
c.add(Age);
age = new JTextField(20);
c.add(age);
Admission = new JLabel("Admission ");
c.add(Admission);
admission = new JTextField(20);
c.add(admission);
Wardname = new JLabel("Ward Name ");
c.add(Wardname);
wardname = new JTextField(20);
c.add(wardname);
Wardnumber = new JLabel("Ward Number ");
c.add(Wardnumber);
wardnumber = new JTextField(20);
c.add(wardnumber);
setSize(400, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent u)
{
Find();// call the find method
}
public void Find()//find method
{
try{
BufferedReader d = new BufferedReader(new FileReader("patient.txt"));
String str ="";
while((str = d.readLine())!=null)
{
StringTokenizer stz = new StringTokenizer(str," ");
if(stz.nextToken().equals(name.getText()))
{
mrn.setText((String)stz.nextToken());
address.setText((String)stz.nextToken());
age.setText((String)stz.nextToken());
admission.setText((String)stz.nextToken());
wardname.setText((String)stz.nextToken());
wardnumber.setText((String)stz.nextToken());
}
}
}
catch(Exception u)
{
JOptionPane.showMessageDialog(this,"Can't Open File, Please Consult a Member of Staff");
}
}
}