// Implementation of problem 11.13 on page 447, use an ArrayList to store input values from the user // but not duplicates import java.util.*; // needed for both ArrayList and Scanner public class Prog11_13 { public static void main(String[] args) { ArrayList list = new ArrayList(); Scanner in = new Scanner(System.in); int temp; Integer boxedTemp; // we will box the int value and see if its a duplicate before adding it to the ArrayList System.out.print("Enter int values, negative to exit "); temp = in.nextInt(); while(temp>=0) { boxedTemp=new Integer(temp); // box the int value if(list.contains(boxedTemp)) System.out.println("duplicated value detected"); // list.contains(Object o) tests to see if o is in list yet, if so, output warning else list.add(boxedTemp); // otherwise add it to the list System.out.print("Enter int values, negative to exit "); temp = in.nextInt(); } System.out.println("\n\n" + list); // we can output an entire ArrayList with one instruction, the ArrayList's output looks like this [1, 2, 3, 4, 5] } }