UPDATE! I have now fixed the original problem, although as a result I have an error with displaying the youngest and oldest member and throws the following error. Any suggestions? Thanks
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at NameAge.main(NameAge.java:46)
import java.util.Scanner;
import java.util.ArrayList;
public class NameAge {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int MAX_VALUE = 4;
ArrayList<String> nameList = new ArrayList<String>();
ArrayList<Integer> ageList = new ArrayList<Integer>();
Integer[] ages = new Integer[10];
for (int i = 0; i < MAX_VALUE; i++) {
System.out.print("Enter a name: ");
String currentLine = input.next();
if (currentLine.equals("DONE")) {
break;
}
nameList.add(currentLine);
System.out.print("Now enter an age for " + currentLine + ": ");
ageList.add(input.nextInt());
}
System.out.print("\n");
for(int i = 0; i < MAX_VALUE; i++) {
System.out.println("Name: " + nameList.get(i) + " Age: " + ageList.get(i));
}
// DISPLAY YOUNGEST AND OLDEST OF ARRAY, PRODUCING ERRORS
int smallest = ageList.get(0);
int largest = ageList.get(0);
String oldest = nameList.get(0);
String youngest = nameList.get(0);
for (int i = 0; i < ages.length; i++) {
if(ageList.get(i) > largest) {
largest = ageList.get(i);
oldest = nameList.get(i);
}
else if(ageList.get(i) < smallest) {
smallest = ageList.get(i);
youngest = nameList.get(i);
}
}
System.out.println("\nThe youngest person is " + youngest + " who is " + smallest + " years old");
System.out.println("The oldest person is " + oldest + " who is " + largest + " years old");
}
}