The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Reading and searching a text file

Posted by Mark on November 24, 2001 at 4:18 AM

Hi

I�m trying to do a program in Java that reads a text file using methods, and then modify the program so that it searches for a name in the Name field. I am very new to Java, and have been searching the Internet to find what commands are needed to read a text file. All the code I have found use one class and a main method. I have enclosed the code I have done so far..not much of an order to it yet! I would appreciate any advice you can give me on how to construct all these methods...especially if someone could get the program working for me! The two questions I�m trying to do are-

Create a text file (using notepad or similar) that contains a series of records of the form:-
SURNAME
FIRSTNAME
ADDRESS
PHONENUMBER
Create a Console Application that has a main method. Add a second class to provide storage for the record structure you have created. Write a method, ReadFirstRecord, that can read the first record from disk and return it to the calling method. Write another procedure, ReadNextRecord, to read the next record from an already open file. The methods should either return a Boolean Success flag or use Exception Handling to report success or failure, if no records are found display a message to report this. Write a loop in the main method to read through the file and display the contents.

Make a copy of the above project. Now create two methods that call your ReadFirstRecord and ReadNextRecord methods to read a record and test to see if the NAME field contains a specific name. Create another method FindName that searches for a name, if the name is not found in the whole file then display a message to show this, otherwise display each record you find. You should create a method to display a record, not code this in the FindName method.

Code I have done so far for the fist question-

public class ReadRecord
{
public String Surname, FirstName, Address, PhoneNumber;

ReadRecord(){
Surname=" ";
FirstName=" ";
Address=" ";
PhoneNumber=" ";
}
}

//public static void main(String[] argv)

{
ReadRecord stRecord = new ReadRecord();
DataInputStream diStream = null;

try {
File fileOpen = new File("Records.txt");
FileInputStream fiStream = new FileInputStream(fileOpen);
BufferedInputStream bufStream = new BufferedInputStream(fiStream);
diStream = new DataInputStream(bufStream);
stRecord = ReadFirstRecord(diStream, stRecord);
System.out.println("First line: "+stRecord.Surname);

while(stRecord.FirstName != null)
{
stRecord = ReadNextRecord(diStream, stRecord);
}
}//end try

catch(IOException e)
{
System.out.println("IOException error: "+e.getMessage());
}

finally
{
diStream.close();
}

public ReadRecord ReadFirstRecord(DataInputStream dis, ReadRecord stRecord)

try {

stRecord.Surname = dis.readline();
stRecord.FirstName = dis.readline();
stRecord.Address = dis.readline();
stRecord.PhoneNumber = dis.readline();
}//end try

{
catch(IOException e)
}
{
System.out.println("IOException error: "+e.getMessage());
}//end catch

return stRecord;
}//end of ReadFirstRecord

public ReadRecord ReadNextRecord(DataInputStream dis, ReadRecord stRecord)

try {

Thanks in advance for any help.

Mark




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us