thx so much for the response guys ... i am able to solve my problem to a great extent ... but now i cannot sort on the gender ...
i want this output :-
Output 1:
Hingis Martina Female 4/2/1979 Green
Kelly Sue Female 7/12/1959 Pink
Kournikova Anna Female 6/3/1975 Red
Seles Monica Female 12/2/1973 Black
Abercrombie Neil Male 2/13/1943 Tan
Bishop Timothy Male 4/23/1967 Yellow
Bonk Radek Male 6/3/1975 Green
Bouillon Francis Male 6/3/1975 Blue
Smith Steve Male 3/3/1985 Red
but the output which i am getting is :-
Output 1:Kelly Sue Female01/12/1959 PinkAbercrombie Neil Male01/13/1943 TanBishop Timothy Male01/23/1967 YellowKournikovaAnnaFemale06/03/1975RedHingisMartinaFemale04/02/1979GreenSelesMonicaFemale12/02/1973BlackSmithSteveMale01/03/1985RedBonkRadekMale01/03/1975GreenBouillonFrancisMale01/03/1975Blue
i cannot sort on the gender ... this is only the problem now ...
I have three files each having 3 records in the form :-
Last Name, First Name, Gender, Favorite Color, Date Of Birth
the second and third files have records of the following format
Last Name, First Name, Middle Initial, Gender, Date Of Birth, Favorite Color
Last Name, First Name, MiddleInitial, Gender, Favorite Color, Date Of Birth
in the second and third files i am reading the gender at the third position in the array becuase they have 6 records and in the first file it has 5 records ... so in order to read gender at third postion in the first file i added a null padding in the array so i can sort properly on gender ... but still no luck .... please help me guys
the following is my code :-
main class
[code]
import java.io.;
import java.sql.;
import java.lang.;
import java.util.;
import java.text.*;
import java.util.Vector;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class test1 {
public static void main(String[] args)
{
try {
ArrayList<Person> personList = new ArrayList<Person>();
// reading data from three files
BufferedReader comma = new BufferedReader(new FileReader("C:\\comma.txt"));
BufferedReader pipe = new BufferedReader(new FileReader("C:\\pipe.txt"));
BufferedReader space = new BufferedReader(new FileReader("C:\\space.txt"));
String c;
String p="";
String s;
String paddedTokens[] = {" "," "," "," "," "," "};
Person person =new Person();
while((c = comma.readLine()) != null )
{
String tokens[] = c.split(",");
try
{
paddedTokens[0] = tokens[0];
paddedTokens[1] = tokens[1];
paddedTokens[2] = " ";
paddedTokens[3] = tokens[2];
paddedTokens[4] = tokens[3];
paddedTokens[5] = tokens[4];
String strDate = paddedTokens[5];
System.out.println("Array Element 1" +paddedTokens[0]);
System.out.println("Array Element 2" +paddedTokens[1]);
System.out.println("Array Element 3" +paddedTokens[2]);
System.out.println("Array Element 4" +paddedTokens[3]);
System.out.println("Array Element 5" +paddedTokens[4]);
System.out.println("Array Element 6" +paddedTokens[5]);
SimpleDateFormat sdfSource = new SimpleDateFormat("mm/dd/yyyy");
Date strnDate = sdfSource.parse(strDate);
Person personcom = new Person();
personcom.setLast_name(paddedTokens[0]);
personcom.setFirst_name(paddedTokens[1]);
personcom.setGender(paddedTokens[3]);
personcom.setColor(paddedTokens[4]);
personcom.setDate(strnDate);
personList.add(personcom);
}
catch(ParseException e)
{
System.out.println("Invalid Date Parser Exception ");
e.printStackTrace();
}
}
while((p = pipe.readLine()) != null )
{
String delims = "[ | ]+";
String tokens[] = p.split(delims);
String strDate = tokens[5];
String strnewDate = strDate.replace("-","/");
try
{
SimpleDateFormat sdfSource = new SimpleDateFormat("mm/dd/yyyy");
Date strnDate = sdfSource.parse(strnewDate);
String strgender = tokens[3];
String strnewgender = strgender.replace("M","Male");
Person personpipe = new Person();
personpipe.setLast_name(tokens[0]);
personpipe.setFirst_name(tokens[1]);
personpipe.setGender(strnewgender);
personpipe.setColor(tokens[4]);
personpipe.setDate(strnDate);
personList.add(personpipe);
}
catch(ParseException e)
{
System.out.println("Invalid Date Parser Exception ");
e.printStackTrace();
}
}
System.out.println("List" + personList);
while((s = space.readLine()) != null )
{
String tokens[] = s.split(" ");
String strDate = tokens[4];
String strnewDate = strDate.replace("-","/");
try
{
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date formDate = df.parse(strnewDate);
String strgender = tokens[3];
String strnewgender = strgender.replace("F","Female");
Person personspc = new Person();
personspc.setLast_name(tokens[0]);
personspc.setFirst_name(tokens[1]);
personspc.setGender(strnewgender);
personspc.setColor(tokens[5]);
personspc.setDate(formDate);
personList.add(personspc);
}
catch(Exception e)
{
System.out.println("Invalid Date Parser Exception ");
e.printStackTrace();
}
}
Iterator it=personList.iterator();
while(it.hasNext())
{
Person itperson =(Person) it.next();
}
// Collections.sort(personList,new DateComparator());
Collections.sort(personList,new GenderComparator());
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
FileWriter writer = new FileWriter("output.txt");
writer.write("Output 1:");
it=personList.iterator();
while(it.hasNext()){
Person itperson =(Person) it.next();
writer.write(itperson.getLast_name());
writer.write(itperson.getFirst_name());
writer.write(itperson.getGender());
writer.write(df.format(itperson.getDate()));
writer.write(itperson.getColor());
}
Collections.sort(personList,new DateComparator());
writer.write("Output 2:");
it=personList.iterator();
while(it.hasNext())
{
Person itperson =(Person) it.next();
writer.write(itperson.getLast_name());
writer.write(itperson.getFirst_name());
writer.write(itperson.getGender());
writer.write(df.format(itperson.getDate()));
writer.write(itperson.getColor());
}
ListIterator itcmp = personList.listIterator();
Collections.sort(personList,new LastNameComparator());
writer.write("Output 3:");
itcmp=personList.listIterator();
while(itcmp.hasNext())
{
Person itperson =(Person) itcmp.next();
}
while(itcmp.hasPrevious())
{
Person itperson =(Person) itcmp.previous();
writer.write(itperson.getLast_name());
writer.write(itperson.getFirst_name());
writer.write(itperson.getGender());
writer.write(df.format(itperson.getDate()));
writer.write(itperson.getColor());
}
comma.close();
pipe.close();
space.close();
writer.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}[/code]
gender comparator :-
[code]
import java.io.;
import java.sql.;
import java.lang.Object;
import java.util.Vector;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
public class GenderComparator implements Comparator{
public int compare(Person e1, Person e2) {
int result;
result = e1.getGender().compareTo(e2.getGender());
if (result != 0) {
return result;
}
return result;
}
}
[/code]
Person class
[code]
import java.io.;
import java.sql.;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Person
{
private String first_name;
private String middle_init;
private String last_name;
private String gender;
private String color;
private Date date;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getMiddle_init() {
return middle_init;
}
public void setMiddle_init(String middle_init) {
this.middle_init = middle_init;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
}
[/code]
Last Name Comparator
[code]
import java.io.;
import java.sql.;
import java.lang.Object;
import java.util.Vector;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class LastNameComparator implements Comparator {
public int compare( Person a, Person b )
{
return a.getLast_name().compareTo(b.getLast_name());
}
}
[/code]
Date Comparator :-
[code]
import java.util.Vector;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
public class DateComparator implements Comparator{
public int compare( Person a, Person b )
{
return( (Date)a.getDate() ).compareTo( (Date) b.getDate() );
}
}
[/code]