Search code examples
javaarraysparallel-arrays

how do i match the index of two parallel arrays?


i have a list in a text file that alternates between name and age. there are 9 names and 9 ages.

i need my program to display

Person (insert name here) has the highest age: (insert age here).

here is my code so far:

     public class Names 
     {


public static void main(String[] args) throws Exception
{

      // get textfile
      Scanner input = new Scanner( new File("names_and_ages.txt") );


final String[] names= new String[9];
final int[] ages= new int[9];
int counter = 0;

while (input.hasNext()) //while not end-of-file
{
    names[counter]=input.next();  
    ages[counter]=Integer.parseInt(input.next());
    counter++;

}
highestAge(names, ages);
input.close();
}//end of main

public static void highestAge (String[] name, int[] age)
{
    String name;
int count = 0;
int oldest = highestAge[count];
for ( int number: highestAge)
{
    if (number > oldest)
        {
        oldest = number;
        }
}
System.out.print("Person " + name + " has the highest age: " + oldest );
}//end of Size method

}//end of class

everything compiles i just can't seen to make the name match the age. help?


Solution

  • If you created a Person class, you could save yourself the trouble of managing two arrays.

    This would be your Person class:

    class Person {
        String name;
        int age;
    }