1

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");

        }

    }
apapapaa
  • 27
  • 6

2 Answers2

2

As the comments suggest, I would use two regular expressions here, one for the name and one for the age. Also, I would take input from the scanner as string. Currently, you are calling Scanner.nextInt(), which I believe would fail for non numeric data. If this happens, your normal application logic to check that input would never even get hit. Instead, read both as string, then use regex to verify the format of the inputs.

Something like this should work:

String age = "35";
String name = "Tim Biegeleisen";

if (name.matches(".*[^a-zA-Z].*")) {
    System.out.println("Not a valid name");
}
if (age.matches(".*[^0-9].*")) {
    System.out.println("Not a valid age");
}
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
1

Regex as pointed out is the easiest way to get your validation done. You may also want to look at the javadoc of java.util.regex.Pattern ( https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html ).

name.matches("\\D+") // for anything other than numbers with length > 1

name.matches("\\d+") // for numbers of length 1 and more
Roland
  • 18,065
  • 1
  • 39
  • 71