Solved

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

Posted on 2004-08-24
9
209 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
ID: 11889420
oRAF.seek(0);
0
 
LVL 1

Author Comment

by:TommyN14
ID: 11889522
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
ID: 11889551
add it where you need to rewind to the beginning of the file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11889784
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:TommyN14
ID: 11895922
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
ID: 11898177
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
ID: 11898341
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
ID: 11898382
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
ID: 11898529
Could you please give me some sample codes?  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

896 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

19 Experts available now in Live!

Get 1:1 Help Now