Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how do i match the index of two parallel arrays?
- 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
- class Person {
- String name;
- int age;
- }
- 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
- 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
- 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;
- }
- }
- if (person.isOlderThen(oldest)) {
- oldest = person;
- }
- class person
- {
- String name;
- int age;
- }
- Person getOldest( Person[] persons )
- {
- Person oldest = null;
- for( Person person : persons )
- {
- if( oldest == null || person.age > oldest.age )
- {
- oldest = person;
- }
- }
- return oldest;
- }
Add Comment
Please, Sign In to add comment