Array program help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Array program help

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    Array program help

    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.

    here is the code of what i have as of right now.




    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class nameid extends JFrame
    {
    JLabel NameL, RankL;

    JTextField NameTF, RankTF;

    JButton findnameB, exitB, findidB;
    FindnameButtonHandler FindnameHandler;
    FindrankButtonHandler FindrankHandler;
    ExitButtonHandler exitHandler;

    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 Find rank button
    findrankB=new JButton("Find id");
    FindrankHandler = new FindrankButtonHandler();
    findrankB.addActionListener(FindrankHandler);



    //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();
    }
    }

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    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.

  3. #3
    Join Date
    Oct 2004
    Posts
    1
    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.

    when u get it, all ur promblems will gone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center