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

    Default 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
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Add a stachtrace to the exception block so you see what is really going wrong, but I am willing to bet that if you use the full path to the file the problem goes away. As the current working directory is probably not what you think it is.

  3. #3
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    You could start by outputting the exception's message for debugging, which could tell you enough about what's wrong to fix it:

    Java Code:
    JOptionPane.showMessageDialog(this,"Can't Open File, Please Consult a Member of Staff"
       + u.toString());
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

Similar Threads

  1. Problem with string tokenizer
    By twinytwo in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2009, 11:27 AM
  2. String Tokenizer And Arraya
    By everlast88az in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 11:20 PM
  3. Store String Tokenizer o/p onto an array
    By rajchief in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 09:00 PM
  4. Parsing or Tokenizer??
    By hiklior in forum New To Java
    Replies: 15
    Last Post: 05-28-2008, 02:20 PM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05: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
  •