Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-24
9
Medium Priority
?
249 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

916 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