0

Say I have 2 Scanner filled arrays, name[] and age[]. Each one filled in order. If I am to find the oldest person in the array how do I print out their name AND their age, using the arrays? For example the largest entry in age[] was 78. Is there a way to relate it with the name[] array to print it out?.

Reference code:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many entries ?");

    int entries;
    do {
        entries = input.nextInt();
        if (entries < 1) {
            System.out.println("please enter a valid number!");
        }
    } while (entries < 1);

    String[] name = new String[entries];
    String[] gender = new String[entries];
    int[] age = new int[entries];

    for (int i = 0; i < entries; i++) {
        System.out.println("Enter the name of person No" + (i + 1) + ".");
        name[i] = input.next();
    }

    double ageSum = 0;
    int max = 0;
    for (int i = 0; i < entries; i++) {
        System.out.println("How old is " + name[i] + " ?");
        age[i] = input.nextInt();
        ageSum += age[i];
        max = Math.max(max, age[i]);
    }

    System.out.println("the oldest person is "
            + name[] + " whose " + max + " years old.");
}
DubbleV
  • 23
  • 4

3 Answers3

1

Assuming that your arrays have the same size and the ages corresponding to the names then you can check for the highest age and store the indice of the element with the highest age.

Then you have your name at this indice.

int highestAgeIndice = 3; //indice of element with age 97 as example

names[highestAgeIndice] // the corresponding name

Calculating highest age and store its indice

int max = 0;
int highestInd = 0;
for (int i = 0; i < age.length; i++) {
    if (age[i] > max) {
        max = age[i];
        highestInd = i;
    }
}

System.out.println("the oldest person is " +
        name[highestInd] + " whose " + max + " years old.");

The Code

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many entries ?");

    int entries;
    do {
        entries = input.nextInt();
        if (entries < 1) {
            System.out.println("please enter a valid number!");
        }
    } while (entries < 1);

    String[] name = new String[entries];
    String[] gender = new String[entries];
    int[] age = new int[entries];

    for (int i = 0; i < entries; i++) {
        System.out.println("Enter the name of person No" + (i + 1) + ".");
        name[i] = input.next();
    }

    double ageSum = 0;
    for (int i = 0; i < entries; i++) {
        System.out.println("How old is " + name[i] + " ?");
        age[i] = input.nextInt();
        ageSum += age[i];
    }

    int max = 0;
    int highestInd = 0;
    for (int i = 0; i < age.length; i++) {
        if (age[i] > max) {
            max = age[i];
            highestInd = i;
        }
    }
    System.out.println("the oldest person is " +
            name[highestInd] + " whose " + max + " years old.");
}
Community
  • 1
  • 1
Aalexander
  • 4,987
  • 3
  • 11
  • 34
1

If you have two arrays name[] and age[], you can relate them by creating some class Person with fields of type the entries in these arrays, and get a list of persons List<Person>, something like this:

static class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return name; }
    public int getAge() { return age; }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}
public static void main(String[] args) {
    String[] name = {"Junior", "Senior", "Middle"};
    int[] age = {25, 78, 40};

    List<Person> people = IntStream.range(0, name.length)
            .mapToObj(i -> new Person(name[i], age[i]))
            .collect(Collectors.toList());

    // sort by age in reverse order
    people.sort(Comparator.comparing(
            Person::getAge, Comparator.reverseOrder()));

    // output
    people.forEach(System.out::println);
}

Output:

Person{name='Senior', age=78}
Person{name='Middle', age=40}
Person{name='Junior', age=25}

See also: How do I sort two arrays in relation to each other?

0

You could use indexOf on you array age for the Max age which will tell you the index of the associated name.

names[age.indexOf(Max)]

JTorres
  • 24
  • 2
  • I got an error saying: "Cannot invoke indexOf(int) on the array type int[]" – DubbleV Feb 11 '21 at 20:19
  • You’re right, my apologies. I was just answering a question on array of strings and that function is for strings or ArrayLists in Java. – JTorres Feb 11 '21 at 20:39
  • 1
    You could just declare a variable index before the for loop and when you set max, set the index variable to your current I in the loop. That’ll tell you which index you set your max from. If that helps, don’t forget to upVote!:D – JTorres Feb 11 '21 at 20:40