I have a person class
public class Person implements Comparable<Person> {
//Instance Variables
private String firstName;
private String lastName;
private int age;
private char gender;
private String ssn;
// This is the constructor of the class Person
public Person(String firstName, String lastName, int age, char gender, String ssn)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age){
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender){
this.gender = gender;
}
public String getSSN() {
return ssn;
}
public void setSSN(String ssn){
this.ssn = ssn;
}
/* make Person talk */
public void talk(){
System.out.println("Blah Blah Blah");
}
public String toString() {
return "Person[firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + ", gender=" + gender + ", ssn=" + ssn + "]";
}
public String GetFullName() {
return "The Full name is : " + firstName + " " + lastName;
}
public String GetLastName() {
return lastName;
}
public int compareTo(Person person) {
return this.lastName.compareTo(person.getLastName());
}
}
I want to use this person class and pull names from a text file of names and then find the matching last names and give the count. Finally I want to take the macthing last names and change them to "Brown". Here is what I have so far...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
public class PeopleSorter {
public static void main(String[] args) {
File personFile;
Scanner fileScanner;
ArrayList<Person> personList = new ArrayList<Person>();
Person tempPerson;
try {
personFile = new File("people.txt");
fileScanner = new Scanner(personFile);
System.out.println("*********************Before Sorting*************************");
while (fileScanner.hasNextLine()) {
String[] personAttributes = fileScanner.nextLine().split(",");
tempPerson = new Person(personAttributes[0].trim(),personAttributes[1], new Integer(personAttributes[2]).intValue(),
personAttributes[3].charAt(0), personAttributes[4]);
personList.add(tempPerson);
System.out.println(tempPerson.toString());
}
System.out.println("*********************After Sorting**************************");
Collections.sort(personList);
for (Person person: personList) {
System.out.println(person.toString());
}
for (Person person: personList) {
System.out.println(person.GetLastName());
}
fileScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I know I should be using countMatches and a loop statement but I'm stuck.