0

I have a Java program that stores a max of 10 arrayList items of both name (String) and age (integer), the program accepts a max of 10 items unless the user enters 'done' or 'DONE', then outputs the items of the Array.

After the array output, I now want the program to output the youngest and oldest members of the array.

I can't seem to work out how to fetch this data and display it.

Any suggestions?

Thank you.

import java.util.Scanner;
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.List;

public class AgeName {
    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

        List<Pair<String, Integer>> names = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            System.out.print("Enter name " + (i + 1) + ": ");
            String line = input.nextLine();

            if (line.toUpperCase().equals("DONE"))    
                break;

                System.out.print("Enter age for user " + line + ": ");
                Integer age = Integer.parseInt(input.nextLine());

                names.add(new Pair<String, Integer>(line, age));
        }
            System.out.print("\n");
            System.out.print("Names are: " + names );
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
APPAPA
  • 25
  • 1
  • 6

1 Answers1

0

Just add two variables to keep these information :

   Pair<String, Integer> minPair = null;
   Pair<String, Integer> maxPair = null;

and add this in the iteration :

 if (minPair==null || pair.getValue() < minPair.getValue()){
           minPair  = pair;
 }
 if (maxPair==null || pair.getValue() > maxPair.getValue()){
           maxPair  = pair;
 }

This gives :

public class AgeName {
    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

        List<Pair<String, Integer>> names = new ArrayList<>();
          Pair<String, Integer> minPair = null;
          Pair<String, Integer> maxPair = null;

        for (int i = 0; i < 10; i++) {
            System.out.print("Enter name " + (i + 1) + ": ");
            String line = input.nextLine();

            if (line.toUpperCase().equals("DONE"))    
                break;

                System.out.print("Enter age for user " + line + ": ");
                Integer age = Integer.parseInt(input.nextLine());
                Pair<String, Integer> pair = new Pair<String, Integer>(line, age);
                names.add(pair);
                if (minPair==null || pair.getValue() < minPair.getValue()){
                   minPair  = pair;
                }
                if (maxPair==null || pair.getValue() > maxPair.getValue()){
                   maxPair  = pair;
                }
            }

          System.out.print("\n");
          System.out.println("Names are: " + names );
          System.out.println("min is: " + minPair.getKey());
          System.out.println("Max is: " + maxPair.getKey());
      }
    }
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks for the reply. However the code you provided produces errors. If statement "bad operand types for binary operator < " – APPAPA Nov 20 '16 at 15:31
  • Ummm I'm just using one method that is shown above (there are no other classes) and importing the following libraries - import java.util.Scanner; import javafx.util.Pair; import java.util.ArrayList; import java.util.List; – APPAPA Nov 20 '16 at 16:41
  • ah I though `Pair` was a custom class. I added the `javafx` tag in your question. I modified my code by typing `Pair` variable declaration. It should be ok like that. – davidxxx Nov 20 '16 at 16:53