How to check matching int values/ String values in an array?

by Navdeep Singh Jaswal   Last Updated July 12, 2019 05:26 AM

We are asked to write a code and enter name and age for 4 people. After that, we are required to find whether any of the age match each other. Later the question asks whether the names match, but I haven't gotten to that part just yet.

I tried making an array so that I can store the values of the inputted ages. I cannot figure out how I can compare age's values with other elements in the array.

package person;

public class Person 
{
    static int personAge;
    String personName;

    public void setName(String name) {
        name=personName;
    }

    public String getName() {
        return personName;       
    }

    public void setAge(int age){
        age=personAge;
    }

    public int getAge() {
        return personAge;
    }
}

package person;

import java.util.Scanner;

public class PersonMain extends Person
{
    static int [] ageStore=new int [4];

    public static void main(String[] args) 
    {
        for(int i=1;i<5;i++)
        {
            Person person= new Person();

            person.setName(person.personName);

            System.out.println("Please enter name for person " + i );

            Scanner input= new Scanner(System.in); 
            person.personName=input.next();
            Scanner keyboard= new Scanner(System.in);
            System.out.println("Now, input " + person.personName + "'s age" );
            person.setAge(personAge);
            personAge=keyboard.nextInt();
            ageStore[i]=person.personAge;
            System.out.println("Age is " + ageStore[i]);
        }
        //what can help me compare the values of int age in the array? 
    }
}

The end goal here is to print out the names of people that have the same age.



Answers 2


Of course they all have the same age: personAge is declared as static. Remove the static keyword in front of the declaration.

UPDATE: and yes, use getters and setters instead of accessing fields directly.

To summarise:

  • remove the static keyword in front of personAge declaration,
  • declare personAge and personName as private to ensure you use the getters and setters,
  • remove the extends Person in class PersonMain,
  • in method main, use the getters and the setters instead of accessing the fields directly.

UPDATE 2: Oh! yes! some more issues:

  • in the setters, assign the argument to the field, not the other way around (personName=name; and personAge=age).
  • variable i should go from 0 to 3 because the array is declared with a size of 4: for(int i=0;i<4;i++) or better: for(int i=0;i<ageStore.length;i++),
  • move the Scanner declaration and initialisation out of the loop, and use only one of them.
Maurice Perry
Maurice Perry
July 12, 2019 04:50 AM

If you are collecting information that is required for later processing, you need to store all of that information.

At the moment, you are storing the ages in an array, but throwing away the names.

So, instead of keeping an age array like this:

static int [] ageStore=new int [4];

... try keeping an array of Persons. Plus you don't need to keep them outside the main method. So, in the main method:

Person[] persons = new Person[4];

Also, fix your Person class so that the age is not static and the variables are private:

private int personAge;
private String personName;

Now create a single scanner because you don't need one per loop or per input:

Scanner input= new Scanner(System.in);

Then create your loop, but run it from 0 to less than the length of the array to fill (because indexes in Java start at 0):

for (int i = 0; i < persons.length; i++) {

Then make a new Person object to populate:

Person person = new Person();

Then collect the name and age from the user (remembering that i is 1 less than what you want to show to the user):

System.out.println("Please enter name for person " + (i + 1) );
person.setName(input.next());

System.out.println("Now, input " + person.getName() + "'s age" );
person.setAge(input.nextInt());

And add the person to the array:

persons[i] = person;

Then you can close your loop.

So far you should have:

package person;

import java.util.Scanner;

public class PersonMain {

    public static void main(String[] args) {
        Person[] persons = new Person[4];

        Scanner input= new Scanner(System.in);

        for (int i = 0; i < persons.length; i++) {

            Person person = new Person();

            System.out.println("Please enter name for person " + (i + 1) );
            person.setName(input.next());

            System.out.println("Now, input " + person.getName() + "'s age" );
            person.setAge(input.nextInt());

            persons[i] = person;
        }

        // matching code will go here
    }
}

Now we need to do the matching part.

One way of doing that is to check each item against all the items after it in the list. That would look like this:

for (int i = 0; i < persons.length - 1; i++) { // until 'persons.length - 1' because if we look at the last item, it has nothing left to compare against
    Person currentPerson = persons[i];

    for (int j = i + 1; j < persons.length; j++) { // start at the next index (i + 1)

        if (currentPerson.getAge() == persons[j].getAge()) { // we have an age match
            System.out.println(currentPerson.getName() + " has the same age as " + persons[j].getName());
        }

    }
}
Jason
Jason
July 12, 2019 05:15 AM

Related Questions


Updated April 25, 2015 03:11 AM

Updated April 23, 2017 13:26 PM

Updated March 13, 2017 13:26 PM

Updated August 19, 2017 23:26 PM

Updated October 20, 2017 04:26 AM