Results 1 to 3 of 3
  1. #1
    twinytwo is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default 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


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

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    patients.txt and class files place in the same folder ?

    i suggest you to catch difference type of Exception like FileNotFileException, IOException other than just catch Exception
    or print out the message by u.printStackTrace();
    Last edited by mtyoung; 03-26-2009 at 12:28 PM.

  3. #3
    twinytwo is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

Similar Threads

  1. String Tokenizer And Arraya
    By everlast88az in forum Advanced Java
    Replies: 2
    Last Post: 11-07-2008, 12:20 AM
  2. String Problem
    By gnomewise in forum New To Java
    Replies: 1
    Last Post: 10-19-2008, 01:37 PM
  3. Store String Tokenizer o/p onto an array
    By rajchief in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 10:00 PM
  4. Parsing or Tokenizer??
    By hiklior in forum New To Java
    Replies: 15
    Last Post: 05-28-2008, 03:20 PM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •