SHARE
TWEET

Untitled

a guest Jan 12th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. how do i match the index of two parallel arrays?
  2. public class Names
  3.      {
  4.  
  5.  
  6. public static void main(String[] args) throws Exception
  7. {
  8.  
  9.       // get textfile
  10.       Scanner input = new Scanner( new File("names_and_ages.txt") );
  11.  
  12.  
  13. final String[] names= new String[9];
  14. final int[] ages= new int[9];
  15. int counter = 0;
  16.  
  17. while (input.hasNext()) //while not end-of-file
  18. {
  19.     names[counter]=input.next();  
  20.     ages[counter]=Integer.parseInt(input.next());
  21.     counter++;
  22.  
  23. }
  24. highestAge(names, ages);
  25. input.close();
  26. }//end of main
  27.  
  28. public static void highestAge (String[] name, int[] age)
  29. {
  30.     String name;
  31. int count = 0;
  32. int oldest = highestAge[count];
  33. for ( int number: highestAge)
  34. {
  35.     if (number > oldest)
  36.         {
  37.         oldest = number;
  38.         }
  39. }
  40. System.out.print("Person " + name + " has the highest age: " + oldest );
  41. }//end of Size method
  42.  
  43. }//end of class
  44.    
  45. class Person {
  46.     String name;
  47.     int age;
  48. }
  49.    
  50. public static void highestAge(String[] names, int[] ages)
  51. {
  52.     //SET SENSIBLE DEFAULTS TO oldest AND index
  53.     int index = 0;
  54.     int oldest = ages[0];
  55.     //Looping based on index
  56.     for (int i = 0;  i < ages.length; i++)
  57.     {
  58.         if (ages[i] > oldest)
  59.         {
  60.            index = i;
  61.            oldest = ages[i];
  62.         }
  63. }
  64. System.out.print("Person " + names[index] + " has the highest age: " + ages[index] );
  65. }//end of Size method
  66.    
  67. public static void highestAge (String[] names, int[] ages)
  68. {
  69.     String name
  70.     int oldest = 0;
  71.     int oldestIndex = -1;
  72.     for (int index = 0; index < ages.length; index++)
  73.     {
  74.         if (ages[index] > oldest)
  75.         {
  76.             oldest = number;
  77.             oldestIndex = index;
  78.         }
  79.     }
  80.     System.out.print("Person " + names[oldestIndex] + " has the highest age: " + oldest );
  81. }//end of Size method
  82.    
  83. public class Person {
  84.     private String name;
  85.     private int age;
  86.     public Person(String name, int age) {
  87.         this.name = name;
  88.         this.age = age;
  89.     }
  90.  
  91.     public String getName() {
  92.         return name;
  93.     }
  94.  
  95.     public int getAge() {
  96.         return age;
  97.     }
  98. }
  99.    
  100. if (person.isOlderThen(oldest)) {
  101.     oldest = person;
  102. }
  103.    
  104. class person
  105. {
  106.    String name;
  107.    int    age;
  108. }
  109.    
  110. Person getOldest( Person[] persons )
  111. {
  112.    Person oldest = null;
  113.    for( Person person : persons )
  114.    {
  115.       if( oldest == null || person.age > oldest.age )
  116.       {
  117.          oldest = person;
  118.       }
  119.    }
  120.    return oldest;
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top