views:

54

answers:

5

I have a following test file :

Jon Smith 1980-01-01
Matt Walker 1990-05-12

What is the best way to parse through each line of this file, creating object with (name, surname, birthdate) ? Of course this is just a sample, the real file has many records.

A: 

Look at BufferedReader class. It has readLine method. Then you may want to split each line with space separators to construct get each individual field.

kuriouscoder
A: 
 import java.io.*;
 class Record
{
   String first;
   String last;
   String date;

  public Record(String first, String last, String date){
       this.first = first;
       this.last = last;
       this.date = date;
  }

  public static void main(String args[]){
   try{
    FileInputStream fstream = new FileInputStream("textfile.txt");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          while ((strLine = br.readLine()) != null)   {
   String[] tokens = str.split(" ");
   Record record = new Record(tokens[0],tokens[1],tokens[2]);//process record , etc

   }
   in.close();
   }catch (Exception e){
     System.err.println("Error: " + e.getMessage());
   }
 }
}
Orbit
i'll update this code in a minute to include the creation of objects.
Orbit
@Brandon -The question clearly has a 'homework' tag...I don't think you are making @mastodon's life any better by giving away the full solution.
johnbk
eep, apologies for giving away the answer.
Orbit
Although I'm going to use the solution with Scanner what I was planning previously, I choose your answer as it's most thorough.
mastodon
Odd, my answer kind of explains everything without showing any code (which I feel is probably the best reply for a homework question). Ah well, as long as you learn something along the way :-)
injekt
You should get bonus points for having an accepted answer with a net of -1 votes.
Andy
A: 

At first glance, I would suggest the StringTokenizer would be your friend here, but having some experience doing this for real, in business applications, what you probably cannot guarantee is that the Surname is a single name (i.e. someone with a double barrelled surname, not hyphenated would cause you problems.

If you can guarantee the integrity of the data then, you code would be

BufferedReader read = new BufferedReader(new FileReader("yourfile.txt"));
String line = null;
while( (line = read.readLine()) != null) {
   StringTokenizer tokens = new StringTokenizer(line);
   String firstname = tokens.nextToken();
   ...etc etc
}

If you cannot guarantee the integrity of your data, then you would need to find the first space, and choose all characters before that as the last name, find the last space and all characters after that as the DOB, and everything inbetween is the surname.

Codemwnci
+1  A: 

Use a FileReader for reading characters from a file, use a BufferedReader for buffering these characters so you can read them as lines. Then you have a choice.. Personally I'd use String.split() to split on the whitespace giving you a nice String Array, you could also tokenize this string.

Of course you'd have to think about what would happen if someone has a middle name and such.

injekt
+1  A: 
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {
    public static void main(String[] args) {
        //
        // Create an instance of File for data.txt file.
        //
        File file = new File("tsetfile.txt");

        try {
            //
            // Create a new Scanner object which will read the data from the 
            // file passed in. To check if there are more line to read from it
            // we check by calling the scanner.hasNextLine() method. We then
            // read line one by one till all line is read.
            //
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

This:

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();

Could also be changed to

        while (scanner.hasNext()) {
            String line = scanner.next();

Which will read whitespace.

You could do

Scanner scanner = new Scanner(file).useDelimiter(",");

To do a custom delimiter

At the time of the post, now you have three different ways to do this. Here you just need to parse the data you need. You could read the the line, then split or read one by one and everything 3 would a new line or a new person.

Matt