Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-24
9
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 400 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

670 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