Solved

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

Posted on 2004-08-24
9
223 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
arguments to jar 5 33
Glassfish admin console not working 1 32
java mysql insert application 14 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

789 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