/*
 * Author: Jacob Fields
 */

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

class Apr14{
	public static void main(String[] args){
		//Declare an array of integers named arr1 that can hold 9 integers.
		int[] arr1 = new int[9];
		
		//Initialize arr1 with the values 1 - 9
		for (int i = 0; i < arr1.length; i++){
			arr1[i] = i + 1;
		}

		//Print the values of arr1 to the screen.
		printArray(arr1);

		//Set the values in arr1 to 2 - 10
		for (int i = 0; i < arr1.length; i++){
			arr1[i] = i + 2;
		}

		//Print the values of arr1 to the screen.
		printArray(arr1);

		//For each element in arr1 set the new value to 2 times the current value.
		for(int i = 0; i < arr1.length; i++){
			arr1[i] = arr1[i] * 2;
		}

		//Print the values of arr1 to the screen
		printArray(arr1);

		//Read from numbers.txt
		Scanner input = null;

		try{
			input = new Scanner(new File("numbers.txt"));
		}catch(FileNotFoundException e){
			System.out.println("Cannot Find File");
			return;
		}

		input.useDelimiter(",|\n");

		for (int i = 0; i < arr1.length; i++){
			arr1[i] = input.nextInt();
		}

		input.close();
		
		//PrintArray
		printArray(arr1);

		//Compute the number of times the value 2 is found in the array and print the count to the screen.
		int count = 0;
		for (int elm: arr1){
			if(elm == 2){
				count++;
			}
		}
		System.out.println("Count 2: " + count);

		//Compute the number of even elements in arr1 and print the count to the screen.
		count = 0;
		for (int elm: arr1){
			if(elm % 2 == 0){
				count++;
			}
		}
		System.out.println("Count even: " + count);

		//For each element in arr1 set the new value to 2 times the current value.
		for(int i = 0; i < arr1.length; i++){
			arr1[i] = arr1[i] * 2;
		}
		
		printArray(arr1);

		//Write the contents of the array arr1 to a file named numbers.bkup. Write all integers to the same line with spaces between them.

		PrintWriter output = null;

		try{
			output = new PrintWriter(new File("numbers.bkup"));
		}catch(FileNotFoundException e){
			System.out.println("Unable to write to file");
			return;
		}

		for (int elm: arr1){
			output.printf("%d ", elm);
		}

		output.close();

		//Declare a 3x3 array named arr2
		int[][] arr2 = new int[3][3];

		//Read the data in numbers.txt and store the values in arr2.
		
		try{
			input = new Scanner(new File("numbers.txt"));
		}catch(FileNotFoundException e){
			System.out.println("Cannot Find File");
			return;
		}

		input.useDelimiter(",|\n");	
	
		for (int i = 0; i < arr2.length; i++){
			for(int j = 0; j < arr2[i].length; j++){
				arr2[i][j] = input.nextInt();
			}
		}
		
		printArray(arr2);
		
		//For each element in arr2 set the new value to 2 times the current value.
		for(int i = 0; i < arr2.length; i++){
			for(int j = 0; j < arr2[i].length; j++){
				arr2[i][j] = arr2[i][j] * 2;
			}
		}
		
		printArray(arr2);
		//Compute the number of even elements in arr2 and print the count to the screen.
		count = 0;
		for(int[] arr: arr2){
			for(int elm: arr){
				if(elm%2 == 0){
					count++;
				}
			}
		}
		System.out.println("Count even: " + count);
		/*Hard 2d array problem
 * 		Make a program that reads in the array from a file and sorts the values (without Array.sort()) such that row 0 contains values between 0-9, row 1 contains values 10-19, and so on. The rows should also be sorted from least to greatest. 
 *		
 * 		*/
	}
		

	static void printArray(int[] arr){
		for(int elm: arr){
			System.out.printf("%d,", elm);
		}
		System.out.println();
	}

	static void printArray(int[][] array){
		for(int[] arr : array){
			for(int elm : arr){
				System.out.printf("%d ", elm);
			}
			System.out.println();
		}
	}

}
