You are on page 1of 12

Program:

Sample output:
After sorting by last name:
David Baker Box State Trading Corporation
Colin Brown Easy Rug Cleaners
Jennifer Davis Quicker Span Reviews
Mary Edwards Simple Byte Technologies
Ruby Flynn Snap Sun Services
Sheila Garcia Clear Blue Window Cleaning
Susan Hernandez Light Table Bearings
Lee Hill Round Rock Engineers
Douglas Ingram Quicker Cupcakes
Rosy King Shots Manufacturing
Conner Lee Cash Pad Loans
Bob Lewis Code Planet Properties
Colt Lopez Case Electronics
Michelle Martin Diamond Post Surveys
Zion Miller Switch Pen Company
Calvin Morris Zippy Sandwiches
Vicky Perez Lime Cake Records
Stephen Phillips Brown Technologies
Willa Roberts Low Sky Seating Company
Chris Robinson Finch Products
Catherine Smith Side Sauce Spices
Brooklynn Taylor Prism Globe Inc
Eddie Vazquez Osweep Industries
Tiana Williams Country Boxes
After sorting by ZIP:
Eddie Vazquez Osweep Industries
Michelle Martin Diamond Post Surveys
Ruby Flynn Snap Sun Services
Colin Brown Easy Rug Cleaners
Lee Hill Round Rock Engineers
Tiana Williams Country Boxes
Susan Hernandez Light Table Bearings
Chris Robinson Finch Products
Willa Roberts Low Sky Seating Company
Douglas Ingram Quicker Cupcakes
Sheila Garcia Clear Blue Window Cleaning
Bob Lewis Code Planet Properties
Rosy King Shots Manufacturing
Stephen Phillips Brown Technologies
Conner Lee Cash Pad Loans
David Baker Box State Trading Corporation
Calvin Morris Zippy Sandwiches
Jennifer Davis Quicker Span Reviews
Mary Edwards Simple Byte Technologies
Vicky Perez Lime Cake Records
Catherine Smith Side Sauce Spices
Brooklynn Taylor Prism Globe Inc
Colt Lopez Case Electronics
Zion Miller Switch Pen Company
Persons belongs to NY:
Catherine Smith Side Sauce Spices 140 E 4th Street Hempline Island New York NY 10018 212-830-
5063 212-830-8652 catherine@smith.com http://www.catherinesmith.com
Brooklynn Taylor Prism Globe Inc 48 E 13th Blvd Corway Hills New York NY 10001 212-778-
6886 212-778-6875 brooklynn@taylor.com http://www.brooklynntaylor.com
SampleData.txt:
Eddie Vazquez Osweep Industries 14316 Hancock Circle Richdale Anchorage AK 99515 907-456-
8510 907-456-2326 eddie@vazquez.com http://www.eddievazquez.com
Chris Robinson Finch Products 2133 N West Street Deerford Maricopa AZ 85004 602-141-
3716 602-141-2898 chris@robinson.com http://www.chrisrobinson.com
Brooklynn Taylor Prism Globe Inc 48 E 13th Blvd Corway Hills New York NY 10001 212-778-
6886 212-778-6875 brooklynn@taylor.com http://www.brooklynntaylor.com
Zion Miller Switch Pen Company 196 State Ave Lawnworth Middlesex NJ 8861 732-331-
5270 732-331-4107 zion@miller.com http://www.zionmiller.com
Ruby Flynn Snap Sun Services 3175 W Carrier Lane Meadowburg Honolulu HI 96819 808-947-
7855 808-947-7119 ruby@flynn.com http://www.rubyflynn.com
Bob Lewis Code Planet Properties 22652 E Easton St East Fortview Lake Will IL 60410 815-356-
3760 815-356-1330 bob@lewis.com http://www.boblewis.com
Susan Hernandez Light Table Bearings 9630 S City Ave Kingstown
Reservoir Maricopa AZ 85051 602-842-3864 602-842-
4662 susan@hernandez.com http://www.susanhernandez.com
Vicky Perez Lime Cake Records 7421 Jefferson Pkwy Hearthbridge Lehigh PA 18087 610-284-
7632 610-284-5884 vicky@perez.com http://www.vickyperez.com
Lee Hill Round Rock Engineers 884 W Glendale Rd Old Lindenfield Grove Los
Angeles CA 91790 626-849-7849 626-849-2614 lee@hill.com http://www.leehill.com
Mary Edwards Simple Byte Technologies 6122 E Blue Vine Prkwy Upper
Caldbrook Orange FL 32809 407-746-3200 407-746-
1395 mary@edwards.com http://www.maryedwards.com
Jennifer Davis Quicker Span Reviews 2598 Oak Rd East Maymeadow Shelby TN 38112 901-438-
6447 901-438-4022 jennifer@davis.com http://www.jenniferdavis.com
Michelle Martin Diamond Post Surveys 329 Brush Cir NW Belcove Marion OR 97302 503-260-
7108 503-260-7000 michelle@martin.com http://www.michellemartin.com
Conner Lee Cash Pad Loans 116 Rail Way Adamsfield Mahoning OH 44512 330-869-4252 330-
869-4647 conner@lee.com http://www.connerlee.com
Colin Brown Easy Rug Cleaners 1612 W Bay Rd Bridgeing Orange CA 92802 714-661-3939 714-
661-2748 colin@brown.com http://www.colinbrown.com
Sheila Garcia Clear Blue Window Cleaning 152 Broad Ln Portcroft Cove Bexar TX 78205 210-340-
4128 210-340-1086 sheila@garcia.com http://www.sheilagarcia.com
Rosy King Shots Manufacturing 6930 Bass Stream Blvd Old Clarksfax Hennepin MN 55428 763-
426-3083 763-426-2774 rosy@king.com http://www.rosyking.com
David Baker Box State Trading Corporation 6728 Timbuck St Pittsfort Mahoning OH 44406 330-
644-7457 330-644-9322 david@baker.com http://www.davidbaker.com
Willa Roberts Low Sky Seating Company 1519 Hawk Ave Coleford El Paso TX 79925 915-660-
1619 915-660-4618 willa@roberts.com http://www.willaroberts.com
Tiana Williams Country Boxes 24969 Tibbs Circle Christiansmount Los Angeles CA 91355 661-
368-4194 661-368-1035 tiana@williams.com http://www.tianawilliams.com
Douglas Ingram Quicker Cupcakes 3450 Lion Rd Brunsbeach Nueces TX 78408 361-773-
7322 361-773-2874 douglas@ingram.com http://www.douglasingram.com
Colt Lopez Case Electronics 370 Stock Ave Fort Hansville Middlesex NJ 8861 732-553-9625 732-
553-2886 colt@lopez.com http://www.coltlopez.com
Stephen Phillips Brown Technologies 1095 SW 2nd Rd New Hamford Vanderburgh IN 47713 812-
310-3693 812-310-6514 stephen@phillips.com http://www.stephenphillips.com
Catherine Smith Side Sauce Spices 140 E 4th Street Hempline Island New York NY 10018 212-830-
5063 212-830-8652 catherine@smith.com http://www.catherinesmith.com
Calvin Morris Zippy Sandwiches 79 W Broadway Ave Bayway Franklin OH 43215 614-114-
7890 614-114-5010 calvin@morris.com http://www.calvinmorris.com

Note: Make sure that fields are seperated by tab space


Code to copy:
/**ReadFile.java**/

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//Create a Java class to contain each record

class Record implements Comparable<Record>


{

private String FirstName;


private String LastName;
private String Company;
private String Address;
private String City;
private String Country;
private String State;
private int ZIP;
private String Phone;
private String Fax;
private String Email;
private String Web;
//Constructor

Record(String fname, String lname, String comp, String add, String city, String coun, String sta, int z,
String ph, String f, String e, String w)

FirstName=fname;

LastName=lname;

Company=comp;

Address=add;

City=city;

Country=coun;

State=sta;

ZIP=z;
Phone=ph;

Fax=f;

Email=e;

Web=w;

public String getFirstName()


{

return FirstName;
}

public String getLastName()


{

return LastName;
}

public String getCompany()


{

return Company;
}

public String getAddress()


{

return Address;
}

public String getCity()


{

return City;
}

public String getCountry()


{

return Country;
}

public String getState()


{

return State;
}

public int getZIP()


{

return ZIP;
}

public String getPhone()


{

return Phone;
}

public String getFax()


{

return Fax;
}

public String getEmail()


{

return Email;
}

public String getWeb()


{

return Web;
}

//Comparator to sort by Last name

public static final Comparator<Record> lastNameComparator=new Comparator<Record>()


{

public int compare(Record r1,Record r2)


{

return r1.LastName.compareTo(r2.LastName);
}

};

//Comparator to sort by ZIP in descending order

public static final Comparator<Record> zipComparator=new Comparator<Record>()


{

public int compare(Record r1,Record r2)


{

return r2.ZIP-r1.ZIP;
}
};

//Returns a record as a string

public String toString()


{

return FirstName+ "\t"+LastName+"\t"+Company+"\t"+Address+"\t"+


City+"\t"+Country+"\t"+State+"\t"+ZIP+"\t"+

Phone+"\t"+Fax+"\t"+Email+"\t"+Web;

@Override

public int compareTo(Record o)


{

// TODO Auto-generated method stub


return 0;
}

public class ReadFile


{

@SuppressWarnings("unchecked")

public static void main(String args[])


{

try
{

//Step1:

//Use a FileReader wrapped in a BufferedReader to read file

BufferedReader br = new BufferedReader( new FileReader("SampleData.txt") );

String currentLine;

//Create an array list to store the records in the file

ArrayList<Record> file=new ArrayList<Record>();


//Read all records

while ((currentLine=br.readLine())!=null)
{

//Read a record and split the fields using tab delimiter

String fields[]=currentLine.split("\t");

//Create an object for each record

Record
r=newRecord(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5],fields[6],Integer.parseInt(fields[7]),
fields[8],fields[9],fields[10],fields[11]);

//and store the object in the ArrayList

file.add(r);

br.close();

//Step 2: sort the data in ascending order based on the LastName field

Collections.sort(file,Record.lastNameComparator);
//display the following fields:First name,Last name,Company

System.out.println("After sorting by last name: ");


for(Record rec: file)
{

System.out.println(rec.getFirstName()+"\t"+rec.getLastName()+"\t"+rec.getCompany());
}

System.out.println();
//Step 3:sort the data in descending order based on the ZIP field

Collections.sort(file,Record.zipComparator);
//display the following fields:First name,Last name,Company

System.out.println("After sorting by ZIP: ");


for(Record rec: file)
{

System.out.println(rec.getFirstName()+"\t"+rec.getLastName()+"\t"+rec.getCompany());
}

System.out.println();
//Step 4: Display all of the records (and all of the fields) for everyone

//that is in the state "NY."

System.out.println("Persons belongs to NY: ");


for(Record rec: file)
{

if(rec.getState().equals("NY"))
System.out.println(rec);
}

catch(Exception e)
{

System.out.println("Error");
}

You might also like