Question: I need help finding out why my coding is not working. Here is the assignment. One of the fundamental requirements of programs is to use

I need help finding out why my coding is not working. Here is the assignment.

One of the fundamental requirements of programs is to use data stored in permanent storage such as a hard disk file. Programs must be able to read and write this type of data and use appropriate data structures within the program to work with the data. In this assignment, you will write a program to load and manipulate a data file. The data file can be downloaded from the following link: Unit 4 Sample Data.

The file should not be a comma delimited file and should have the following record structure:

FirstName Character Size (12)

LastName Character Size (16)

Company Character Size (32)

Address Character Size (32)

City Character Size (24)

County Character Size (24)

State Character Size (2)

ZIP Number Size (5)

Phone Character Size (12)

Fax Character Size (12)

Email Character Size (32)

Web Character Size (42)

Step 1: For this assignment, you will write a complete Java console program to load the data file into an ArrayList data structure. Store each line from the file as a separate record in the ArrayList. To read the file, you should use a FileReader wrapped in a BufferedReader. If you create a Java class to contain each record, you will be able to create an object for each record and store the object in the ArrayList. Sorting the data can then be done using another wrapper class that contains the ArrayList and has methods for sorting and output of the data. Step 2: After the file is loaded into the ArrayList, sort the data in ascending order based on the LastName field, and display the following fields:

First name

Last name

Company

Step 3: Next, sort the data in descending order based on the ZIP field, and display the following fields:

First name

Last name

Company

ZIP

Step 4: Display all of the records (and all of the fields) for everyone that is in the state "NY."

Step 5: The submission should be a single MS Word document with the program source code for the solution and screenshots of the output for the following list:

Sorted data based on last name

Sorted data based on ZIP

Everyone in the state of NY

*This is my coding below that I can not get to work. I have downloaded the file as a .cvs excel file. The program is not reading the file for some reason. Please help

package itco321unit4;

/** * * @author melis */

import java.io.BufferedReader;

import java.io.FileReader; import java.io.IOException;

import java.util.ArrayList; import java.util.List;

import java.util.Collections;

import java.util.Comparator;

//Create a Java class to contain each record

class Record implements Comparable

{

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 lastNameComparator=new Comparator()

{

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 zipComparator=new Comparator()

{

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 ITCO321Unit4 {

public static void main(String args[])

{

try

{

//Step1:

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

BufferedReader br = new BufferedReader( new FileReader("ITCO321U4IPsampledata.csv") );

String currentLine;

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

ArrayList file=new ArrayList();

//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=new Record(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");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!