Link to home
Start Free TrialLog in
Avatar of Wonder19
Wonder19

asked on

how to read a text file and return a specifc record?

I have a text file with ; as delimiter. Each record contains first name, last name, phone number and extension. How can I search a record with first name as "John" and last name as "Smith".  And how can I search all records if last name is "Smith" ?  

It may sound easy to you, but I have done a lot of searching and can't find the answer. Please help.
Avatar of hoomanv
hoomanv
Flag of Canada image

post first few lines of input file to see what structure it has
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

to read the file

Use a class like this (you'll have to add the fields)

public class Person {
      public Person(String[] args) {
            this.firstName = args[0];
            this.lastName = args[1];
            // (etc)
      }
}

You can make the process method shown at the link

public void process(String line) {
      people.add(new Person(line.split(";")));
}

'people' would be a Set or List you create before the file is read

To find surname 'Smith' you can do

List smiths = new ArrayList();
Iterator i = people.iterator();
while (i.hasNext()) {
      Person p = (Person)i.next();
      if ("Smith".equals(p.getLastName())) {
            smiths.add(p);
      }
}
Avatar of Wonder19
Wonder19

ASKER

Dear CEHJ,

Sorry that I'm so junior in Java programming. I found the URL you posted, but I don't understand well.  Where should I put the open file try catch block in your suggested Person class?  Would you please provide more detail on "'people' would be a Set or List you create before the file is read". Thanks a ton!

try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
            people.add(new Person(line.split(";")));
        }
        in.close();
    } catch (IOException e) {
}
ASKER CERTIFIED SOLUTION
Avatar of ctamrakar
ctamrakar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Where should I put the open file try catch block in your suggested Person class?

You don't put it in the Person class. You can put it in your main class. Call it getPeople say.

public void getPeople() {
    // File reading code here
}

You can put the Set or List in the constructor

public class YourClass {
    private List people;

    public YourClass() {
        people = new ArrayList();
    }

    public void getPeople() {
        // File reading code here
    }
}
Dear ctamrakar,

Sorry to be a pain in the neck. Your comment is very help. But I'm not smart enough to complete this task without your help. I set a String record = "";  then ...
....
if   ( first.equals("firstname") and last.equals("lastname")) {
    // find the record(s) which has the conditions
     record += line;
}      
....
while (line != null);
System.out.println("Record = " + record);

But there is no record returned. I checked my file, firstname & lastname are in the same record and they are spelling the same.  Did write space count? Any idea?




Dear ctamrakar,

I found that if I search for only one matching field, it returns at least a record.  But I can't search a record with two fields in a record.  Did I miss anything?

Dear CEHJ,

Thanks for your explanation. I will keep it in mind.
Dear wonder19,

Can i know how many fields you have in text file ? what is the format of ur file
may be you got some exception and program never went into if loops.



chandan Tamrakar


-----------

Sorry to be a pain in the neck. Your comment is very help. But I'm not smart enough to complete this task without your help. I set a String record = "";  then ...
....
if   ( first.equals("firstname") and last.equals("lastname")) {
    // find the record(s) which has the conditions
     record += line;
}      
....
while (line != null);
System.out.println("Record = " + record);

But there is no record returned. I checked my file, firstname & lastname are in the same record and they are spelling the same.  Did write space count? Any idea?