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.

The basic idea when you're doing anything of this nature is to (assuming you're not going to use a more advanced algorithm) is to take a given item (name, in this case) and compare it with all the other options. The most basic way to do this is to have a loop compare (using some operator, like '==', or using the .equals() function*) the name against all the other names in the ArrayList. This is not the best way to search for things in a List/Array, but it is the most basic.

Seeing as this seems to be an assignment, you shouldn't use this, but there is the contains() function, which would do this sort of thing.

Good luck!

*: There is a difference, as shown here

so my problem is starting my loop.. each element of the arraylist doesn't have a name/number since it's being pulled from a text file. usually i write a loop like ...

    Person oldestPerson = person1;


        for ( Person pers : personList ) {

            //Compare the ages of each person and save teh name of the oldest person
            if (pers.getAge()>oldestPerson.getAge()) {
                    oldestPerson = pers ;
                }
            // Print out the full name of the oldest person
                }
            System.out.println("The name of the oldest Person is: " + oldestPerson.GetFullName());

above is only part of the code but in that example I can name a new object because it is associated with a number. In the code that I'm struggling with it doesn't have a number so I'm ot sure how to name one of the objects to create a new one.

I was thinking of a couple of ways to figure this out...One is add an element to the person called a counter and I will add 1 to the counter when ever there is another name that matches it. the other way is creating a string out of just the last names and going through each part and seeing if it matches. I'm usually so fast at these assignments but some reason I'm completely blocked on this one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.