0

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?

user1556084
  • 29
  • 1
  • 3
  • 12
  • 1
    I don't think this compiles. Missing semicolon on highestAge() method at `String name` Also since you are trying parallel arrays you should do your loops with `for(int i = 0; i < limit; i;++)` format. That way you can save the actual index where the value is found – gtgaxiola Oct 08 '12 at 22:35
  • that's my question- how do i save the actual index? – user1556084 Oct 08 '12 at 22:36
  • why `highestAge()` takes schools and size? do you mean names and ages? – gtgaxiola Oct 08 '12 at 22:38
  • My guess is that it is cargo-cult homework. – Tim Bender Oct 08 '12 at 22:41

4 Answers4

1

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;
}
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
1
public static void highestAge(String[] names, int[] ages)
{
    //SET SENSIBLE DEFAULTS TO oldest AND index
    int index = 0;
    int oldest = ages[0];
    //Looping based on index
    for (int i = 0;  i < ages.length; i++)
    {
        if (ages[i] > oldest)
        {
           index = i;
           oldest = ages[i];
        }
}
System.out.print("Person " + names[index] + " has the highest age: " + ages[index] );
}//end of Size method
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
0

You're not far off. You need to maintain an "index" to the oldest age, which you can then use to reference between the two arrays.

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

Also, I don't know how your example compiles, I don't see the array highestAge declared any where :P

Personally, I'd create a Person class that has the name and age as fields...

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

I would then create a new Person on each iteration of the loop and add them to an array of people (Person[] people = new Person[...]) (actually, I'd use an implementation of a java.util.list but that's me)

You suddenly get a whole lot of creativity opening up for you.

You could provide a isOlderThen(Person person) method to compare people together.

if (person.isOlderThen(oldest)) {
    oldest = person;
}

You actually just sort the array based on the age of each person, using Arrays.sort and a Comparator

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

1) create a class

class person
{
   String name;
   int    age;
}

2) create only one array: Person[] persons

3) Rewrite the method highestAge

Person getOldest( Person[] persons )
{
   Person oldest = null;
   for( Person person : persons )
   {
      if( oldest == null || person.age > oldest.age )
      {
         oldest = person;
      }
   }
   return oldest;
}
Aubin
  • 14,617
  • 9
  • 61
  • 84