I have an error in my code that i cant fix, it deals with the ablist package. This is the code:
import java.io.*;
import java.util.*;
import abListPackage.SortedABList;
public class CSPeople
{
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
// Get user’s display preference
int choice;
System.out.println(” 1: Sorted by name? \n 2: Sorted by year of birth?”);
System.out.print(“\nHow would you like to see the information > “);
choice = scan.nextInt();
// Instantiate sorted list
SortedABList
if (choice == 1)
people = new SortedABList
else
people = new SortedABList
// Set up file reading
FileReader fin = new FileReader(“input/FamousCS.txt”);
Scanner info = new Scanner(fin);
info.useDelimiter(“[,\\n]”); // delimiters are commas, line feeds
FamousPerson person;
String fname, lname, fact;
int year;
// Read the info from the file and add it to the list
while (info.hasNext())
{
fname = info.next(); lname = info.next();
year = info.nextInt(); fact = info.next();
person = new FamousPerson(fname, lname, year, fact);
people.add(person);
}
// Display the list, using the advanced for loop
System.out.println();
for (FamousPerson fp: people)
System.out.println(fp);
info.close();
}
}
import java.util.Comparator;
public class FamousPerson implements Comparable
{
protected String firstName, lastName, fact;
protected int yearOfBirth;
public FamousPerson(String first, String last, int yob, String f)
{
firstName = first; lastName = last; fact = f; yearOfBirth = yob;
}
public String getFirstName() {return firstName ;}
public String getLastName() {return lastName;}
public String getFact() {return fact;}
public int getYearOfBirth() {return yearOfBirth;}
@Override
public boolean equals(Object obj)
// Returns true if ‘obj’ is a FamousPerson with same first and last
// names as this FamousPerson, otherwise returns false.
{
if (obj == this)
return true;
else
if (obj == null || obj.getClass() != this.getClass())
return false;
else
{
FamousPerson fp = (FamousPerson) obj;
return (this.firstName.equals(fp.firstName) &&
this.lastName.equals(fp.lastName));
}
}
public int compareTo(FamousPerson other)
// Precondition: ‘other’ is not null
//
// Compares this FamousPerson with ‘other’ for order. Returns a
// negative integer, zero, or a positive integer as this object
// is less than, equal to, or greater than ‘other’.
{
if (!this.lastName.equals(other.lastName))
return this.lastName.compareTo(other.lastName);
else
return this.firstName.compareTo(other.firstName);
}
@Override
public String toString()
{
return (firstName + ” ” + lastName + “(Born ” + yearOfBirth +
“): ” + fact);
}
public static Comparator
{
return new Comparator
{
public int compare(FamousPerson element1, FamousPerson element2)
{
return (element1.yearOfBirth – element2.yearOfBirth);
}
};
}
// Order alphabetically by name (first name, then last name)
public static Comparator
{
return new Comparator
{
public int compare(FamousPerson element1, FamousPerson element2)
{
if(element1.firstName.compareTo(element2.firstName)!=0)
return element1.firstName.compareTo(element2.firstName);
else
return element1.lastName.compareTo(element2.lastName);
}
};
}
//Order by year of birth—decreasing
public static Comparator
{
return new Comparator
{
public int compare(FamousPerson element1, FamousPerson element2)
{
return (element2.yearOfBirth – element1.yearOfBirth);
}
};
}
// Order by length of “fact”—increasing
public static Comparator
{
return new Comparator
{
public int compare(FamousPerson element1, FamousPerson element2)
{
return (element1.fact.length() – element2.fact.length());
}
};
}
}
The error is located in the CSPeople class.