I have to write a program that will have a gui interface, and will have a data file with information stored on it like this.
John 9503
Fred 1111
MIke 1211
Well the program will allow the user to input either the name of the person or the id # of the person. And the program will basically do a search of the file and then return either name or id depending which one was inputted by the user.
I have already set up the GUI interface for the program but I cannot figure what I need to do next to get the info from the file etc....... I do know that the information from the file has to be read into an array but I can't figure out how to get it done.
Anyone that can help. I will greatly appreciate.
private static final int WIDTH = 450;
private static final int HEIGHT = 200;
public nameid()
{
// Create labels
NameL = new JLabel("Enter the Name: ",
SwingConstants.RIGHT);
RankL = new JLabel("Enter the id#: ",
SwingConstants.RIGHT);
//Create text fields
NameTF = new JTextField(20);
RankTF = new JTextField(20);
//create Find Name Button
findnameB = new JButton("Find Name");
FindnameHandler = new FindnameButtonHandler();
findnameB.addActionListener(FindnameHandler);
//Create Exit Button
exitB = new JButton("Exit");
exitHandler = new ExitButtonHandler();
exitB.addActionListener(exitHandler);
//Set the title of the window
setTitle("Person's name and id#");
//Get the container
Container pane = getContentPane();
//Set the layout
pane.setLayout(new GridLayout(0,2,1,1));
//Place all items created
pane.add(NameL);
pane.add(NameTF);
pane.add(RankL);
pane.add(RankTF);
pane.add(findnameB);
pane.add(findrankB);
pane.add(exitB);
//set the size of the windowand display it
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class FindnameButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
private class FindrankButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main( String args[] )
{
nameid rectObject = new nameid();
}
}
10-25-2004, 02:48 PM
ractoc
You could create a FileReader object and do a line by line readout of the file.
you can then split every line you read on the space character since that seems to be the seperating char between the name and the id.
Finally you could check both the name and the id against the user input.
If you find a match you don't have to look any fuirther do you can close the reader.
10-31-2004, 07:19 PM
burnet
that's a really bad idea to use array, specially when u stored much information.
coz the performance is O(n) when u do searching.
i suggest u to use the Binary Search Tree, which u can check it int ADT.