I am working on an array that stores a string of name and an integer of age, I want the array to be a max of 4 and print out these results, unless the user enters the word 'done', if so the program terminates and the array outputs what it has stored.
The array works fine but can't seem to get the 'done' section, that I am currently using an if statement with .equals
any suggestions?
Thanks
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;
//boolean name = false;
ArrayList<String> nameList = new ArrayList<String>();
ArrayList<Integer> ageList = new ArrayList<Integer>();
Integer[] ages = new Integer[4];
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 member of array
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");
}
}