import java.util.Scanner;
import java.io.*;

/** Lab assignment:   
  * Declare an array of ints. Read in values as long as user wants
  * to enter values and store the values in the array.
  * (hint: in order to do this, you will need a counter that will both
  * count and be used as the index into the array.
  * 
  * Call a method to print all the values in the array.
  * Note: the method should only print the number of elts that you
  * read in.
  */
public class LabArrays {
 
 public static void main(String[] args) throws IOException {
   
   final int SIZE = 15;
   int[] numbers = new int[SIZE];
   // array initialization
   int[] arr = {6, 8, 9, 3, 4, 10};
   int count=readArray(numbers, SIZE); // serve as the index into the array
   
   /***
   numbers[count] = sc.nextInt();
   // while user did not enter -1, AND we are still within bounds
   while (numbers[count]!=-1 && count<SIZE-1) {
     count++;
     numbers[count] = sc.nextInt();
   }
   
   there's a difference between:
   numbers[count]++  this stmt increments the value at numbers[count]
   numbers[count++] this refers to numbers[count] then increments
   the variable count
   
   // alternative
    numbers[count++] = sc.nextInt();
   // while user did not enter -1, AND we are still within bounds
   while (numbers[count-1]!=-1 && count<SIZE-1) {
     System.out.println("Enter an integer, -1 to exit: ");
     numbers[count++] = sc.nextInt();
   }
   ***/
   // alternative -- read the value into an individual var and place
   // it into the array if it's not -1 and not out of bounds
   
   printArray(numbers, count);
   // print entire array
   for (int i=0; i<numbers.length; i++)
     System.out.println(numbers[i]);
   // if you are looping through all elts in an array (until length-1)
   // you can use an ENHANCED FOR LOOP
   System.out.println("Printing using enhanced for loop: ");
   for (int singleNum : numbers) // exactly the same thing as line 50
     System.out.println(singleNum);
 }
   
 /** readArray reads from the keyboard any number of integers
   * and puts them into the array arr.
   * @param arr the array to fill
   * @param SIZE is the capacity of the array
   * @return the number of elements that were read in.
   */
 public static int readArray(int[] arr, final int SIZE) {
   Scanner sc = new Scanner(System.in);
   int count=0;
   System.out.println("Enter an integer, -1 to stop: ");
   int num = sc.nextInt();
   while (num!=-1 && count<SIZE) {
       arr[count]=num;
       count++;
       System.out.println("Enter an integer, -1 to exit: ");
       num = sc.nextInt();
   }
   return count;
}
 
/** printArray prints the values in an integer array on one line */
 public static void printArray(int[] arr, int size) {
   for (int i=0; i<size; i++)
     System.out.print(arr[i] + " ");
   System.out.println();
 }
}
/** Lab Assignment: implement 3 algorithms that we did on chalkboard
  * in Java:
  * 1. Copying arrays
  * 2. Comparing arrays
  * 3. Searching an array
  * It is OK for this all to be done in main.
  * You can intialize arrays with values (with curly braces)
  */





