Solved

Search through the text file and display on a GUI..

Posted on 2004-08-24
9
197 Views
Last Modified: 2010-03-31
Hi,

I have a text file that contain the Employee Name, Address, and so on.  I'm writing a program to search this text file for a specific name and display on a GUI.  I'm able to make it to work, but now the problem I have is if the name is duplicate, I want to display it record by record and I want to be able to move back and forth.  Right now, I have a search button, when I click on it, it will call the searchName function which perform the search.  Once it reach end of file, I have no way to rewind it so it can go back to search from the beginning.  Please help!

Thanks!

public void searchName()
{
         String strName, strName2Search;

         if (oRAF == null)
         {
            try
            {
               oRAF = new RandomAccessFile("C:/Temp/Employee.txt", "rw" );
            }
            catch(IOException e)
            {                              
               System.err.println( "File not opened properly\n" + e.toString() );
               System.exit(1);
            }      
         }
                  
         try
         {
            strName2Search = txtName.getText();
            strName = oRAF.readUTF();
            ...
            strName2Search = strName2Search.trim();
            strName = strName.trim();
            if (strName2Search.equalsIgnoreCase(strName))
            {
      txtName.setText(strName);
      ...
            }
         }
         
         catch(EOFException e2)
         {
            System.err.println("End Of File.");
         }
         catch(IOException e2)
         {
            System.err.println("Error reading file");
            System.exit(1);
         }
      }
0
Comment
Question by:TommyN14
  • 4
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
oRAF.seek(0);
0
 
LVL 1

Author Comment

by:TommyN14
Comment Utility
Hi,

Where do I add that code in?  I tried it in the Catch, and it gave me a compile error
SearchEmp.java:187: unreported exception java.io.IOException; must be caught or declared to be thrown
output.seek(0);

Thanks!
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
add it where you need to rewind to the beginning of the file.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If you only have a reasonably small file, you could hold each Employee as an object in memory and display/navigate with ease. Make an Employee class and create instances from the fields
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:TommyN14
Comment Utility
Well, I would like to do it as soon as the file has reach the end so the next time they click on Search again, it will go to the beginning.

try
         {
            strName2Search = txtName.getText();
            strName = oRAF.readUTF();
            ...
            strName2Search = strName2Search.trim();
            strName = strName.trim();
            if (strName2Search.equalsIgnoreCase(strName))
            {
     txtName.setText(strName);
     ...
            }

Some where in the try but i don't know how.

Thanks!
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
put it at the end in a finally block, that way you can be sure it occurs

        catch(EOFException e2)
         {
            System.err.println("End Of File.");
         }
         catch(IOException e2)
         {
            System.err.println("Error reading file");
            System.exit(1);
         }
         finally
         {
             try
             {
                 oRAF.seek(0);
             }
             catch (IOException ex)
             {
                System.err.println("Error rewinding file");
             }
         }
0
 
LVL 1

Author Comment

by:TommyN14
Comment Utility
objects,

Thanks for your help.  This 100 pts will be granted to you.  I also have another question related to this.  I'll increase pts to 300 total if you can help.  Here is the question.  If i click on the Search button, I want to be able to search through the text file for all matching data, if only one entry was found, then display on the same screen. ( This part is done already)  But if there are more than one entry, I want to pops up another window display all the entries, and when I click on each entry from a new window, It will display the correct data on the Search window.
Please help.  Thanks!
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
keep track of how many matches you have found and create a new frame for all matches >1
0
 
LVL 1

Author Comment

by:TommyN14
Comment Utility
Could you please give me some sample codes?  Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now