The Artima Developer Community
Sponsored Link

Java Answers Forum
How to serach a text file and display the results

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Slinger

Posts: 1
Nickname: slinger
Registered: Apr, 2002

How to serach a text file and display the results Posted: Apr 23, 2002 9:40 PM
Reply to this message Reply
Advertisement
Hi
I have the following code which allows me to add a someone's details to a simple text file. What I am trying to do now is use the search button to let me search the phone number based upon a person's name. I should be able to type in their Surname, click the Search button, and the info be displayed, if the person exists, else return "No Person exists" message.

Thanks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
 
public class Directory extends JFrame 
{
	private JLabel lblHeading;
	private JLabel lblSurname;
	private JLabel lblForename;
	private JLabel lblAddress;
	private JLabel lblBlank;
	private JLabel lblStreet;
	private JLabel lblCity;
	private JLabel lblPostCode;
	private JLabel lblDOB;
	private JLabel lblPhoneNo;
	
	private JTextField txtSurname;
	private JTextField txtForename;
	private JTextField txtAddress;
	private JTextField txtStreet;
	private JTextField txtCity;
	private JTextField txtPostCode;
	private JTextField txtDOB;
	private JTextField txtPhoneNo;
	
	private JButton butAdd;
	private JButton butSearch;
	private JButton butExit;
	
	private JMenuBar bar;
	private JMenu fileMenu;
	private JMenuItem fileAdd;
	private JMenuItem fileSearch;
	private JMenu exitMenu;
	private JMenuItem exitItem;
	
	private JPanel p1;
	private JPanel p2;
	private JPanel p3;
	
	private Container c;
	
	private DataOutputStream ostream;
	
	public Directory()
	{
		super("ACME Directory Management System");
		c=getContentPane();
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		
		lblHeading = new JLabel("ACME Directory");
		
		lblSurname = new JLabel("Surname");
	 	lblForename = new JLabel("Forename");
		lblAddress = new JLabel("Address");
		lblBlank = new JLabel(" ");
		lblStreet = new JLabel("Street");
		lblCity = new JLabel("City");
		lblPostCode = new JLabel("Postcode");
		lblDOB = new JLabel("Date of Birth");
		lblPhoneNo = new JLabel("Phone Number");
		
		txtSurname = new JTextField(10);
		txtForename = new JTextField(10);
		txtStreet = new JTextField(10);
		txtCity = new JTextField(10);
		txtPostCode = new JTextField(10);
		txtDOB = new JTextField(10);
		txtPhoneNo = new JTextField(10);
		
		butAdd = new JButton("Add");
		butSearch = new JButton("Search");
		butExit = new JButton("Exit");
		
		p1.setLayout(new FlowLayout());
		p1.add(lblHeading);
		
		p2.setLayout(new GridLayout(8,2));
		p2.add(lblForename);
		p2.add(txtForename);
		p2.add(lblSurname);
		p2.add(txtSurname);
		p2.add(lblAddress);
		p2.add(lblBlank);
		p2.add(lblStreet);
		p2.add(txtStreet);
		p2.add(lblCity);
		p2.add(txtCity);
		p2.add(lblPostCode);
		p2.add(txtPostCode);
		p2.add(lblDOB);
		p2.add(txtDOB);
		p2.add(lblPhoneNo);
		p2.add(txtPhoneNo);
		
		p3.setLayout(new GridLayout(1,2));
		p3.add(butAdd);
		p3.add(butSearch);
		p3.add(butExit);
		
		c.add(p1,BorderLayout.NORTH);
		c.add(p2,BorderLayout.WEST);
		c.add(p3,BorderLayout.SOUTH);
		
		bar = new JMenuBar();
		fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		fileAdd = new JMenuItem("Add");
		fileAdd.setMnemonic('A');
		fileAdd.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				addRecord();
			}
		});		
		fileSearch = new JMenuItem("Search");
		fileSearch.setMnemonic('S');
		fileSearch.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				searchRecord();
			}
		});		
		exitMenu = new JMenu("Exit");
		exitMenu.setMnemonic('X');
		exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('X');
		exitItem.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					System.exit(0);
				}
			}
		);		
		fileMenu.add(fileAdd);
		fileMenu.add(fileSearch);
		exitMenu.add(exitItem);
		bar.add(fileMenu);
		bar.add(exitMenu);
		setJMenuBar(bar);
		
		
		try
		{
			ostream = new DataOutputStream(new FileOutputStream("c:\\temp\\data.txt"));
		}
		catch(IOException ioe)
		{
			System.out.println("Error Opening File");
			System.exit(1);
		}
		
		ButtonHandler buttonHandler = new ButtonHandler();
		butAdd.addActionListener(buttonHandler);
		butSearch.addActionListener(buttonHandler);	
		butExit.addActionListener(buttonHandler);
	}
	
	
	class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == butExit)
			{
				System.exit(0);
			}
			else	
			if (e.getSource() == butAdd)
				addRecord();
			else
				searchRecord();
		}
	}
	
	public void addRecord()
	{
		try
		{
			String sname = txtSurname.getText();
			String fname = txtForename.getText();
			String street = txtStreet.getText();
			String city = txtCity.getText();
			String postcode = txtPostCode.getText();
			String dob = txtDOB.getText();
			int phoneno = Integer.parseInt(txtPhoneNo.getText());
			
			ostream.writeUTF(sname);
			ostream.writeUTF(fname);
			ostream.writeUTF(street);
			ostream.writeUTF(city);
			ostream.writeUTF(postcode);
			ostream.writeUTF(dob);
			ostream.writeInt(phoneno);			
					
			JOptionPane.showMessageDialog(null,"Record is added","Add",JOptionPane.PLAIN_MESSAGE);
		}
		catch(NumberFormatException nfe)
		{
			JOptionPane.showMessageDialog(null,"Error in data","Format",JOptionPane.ERROR_MESSAGE);
		}
		catch(IOException ioe)
		{
			JOptionPane.showMessageDialog(null,"Error adding record","Add",JOptionPane.ERROR_MESSAGE);
		}
	}
		
	
	
	public void searchRecord()
	{
	
	}
	
	
	public static void main(String args[])
	{
		Directory d = new Directory();
		d.setSize(300,300);
		d.show();
		d.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});	
	}
}

Topic: Windows 2000 Server Active directory and Java Previous Topic   Next Topic Topic: repaint parts of JTable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use