Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

it drives me crazy... The file contains integers, each on a separate line. The f

ID: 3541124 • Letter: I

Question

it drives me crazy...


The file contains integers, each on a separate

line. The first line of the input file will contain the number of integers in the file. You then create a

corresponding array and fill the array with integers from the remaining lines. If the input file does not

exist, give an appropriate error message and terminate the program. After integers are stored in an array,

your program should call the following methods in order, output the intermediate results and count

statistics on screen, and at end output even integers and odd integers to two different files called

even.out and odd.out


this is my assignment.


i worte the java code and try to find errors.


even if there are no error, it doesnt work at all .


the result is


Exception in thread "main" java.io.FileNotFoundException: data.in (The system cannot find the file specified)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream.<init>(Unknown Source)

at java.util.Scanner.<init>(Unknown Source)

at cs.dddd.main(dddd.java:17)

=======  


import java.util.Scanner;

import java.io.File;

import java.io.PrintWriter;

import java.io.*;



public class ArrayProcessing

{

   public static void main(String[] args)

   {


   inputData();

   printArray(inputData());

   reverseArray(inputData());

   System.out.print("The sum of all elements = ");

   sum(inputData());

   System.out.println();

   System.out.print("The average of all elements = ");

   average(inputData());

   System.out.println();

   System.out.print("max = ");

   max(inputData());

   System.out.println();

   System.out.print("min = ");

   min(inputData());

   System.out.println();

   System.out.println();

   System.out.println("Done, plese check files 'even.out and 'odd.out'.");

   outputData(inputData());


   }

   public static int[] inputData()

   {

   int SIZE;

   int[] array = new int[SIZE];

   int index = 0;

   //create a scanner object

   Scanner keyboard = new Scanner(System.in);


   //get the file name

   System.out.println("enter input filename:  ");

   System.out.println();

   String fileName = keyboard.nextLine();



   //make sure the file exist

   File file = new File(fileName);              //error on file

   Scanner inputFile = new Scanner(file);

   

   if (!file.exists())

   {

       System.out.println("The file " + fileName + "is not found.");

       System.exit(0);

   }


   //open the file

  

   


   //read the array elements to file

   while (inputFile.hasNext() && index < array.length)

   {

       array[index] = inputFile.nextInt();

       index++;

   }


   //close the file

   inputFile.close();


   }


   public static void printArray(int[] array)

   {


   System.out.println("original array: ");


   for(int r = 0; r < array.length; r++)

   {

      for(int c = 0; c <= 10; c++)

      {

         System.out.printf("%f   ",array);

      }


      System.out.println();

   }


   System.out.println();

   }


   public static void reverseArray(int[] array)

   {

   int left = 0;

   int right = array.length -1;


   System.out.println("reversed array:");

   while(left < right)

   {

       int temp = array[left];

       array [left] = array[right];

       array [right] = temp;


       left++;

       right--;


       for(int r = 0; r < array.length; r++)

       {

          for(int c = 0; c <= 10; c++)

          {

             System.out.printf("%f   ",array);

          }


          System.out.println();

       }


       System.out.println();


   }



   }


   public static int sum(int[] array)

   {

   int total = 0;


   for(int i = 0; i < array.length; i++)

   {

       total += array[i];          

   }


   return total;

   }


   public static double average(int[] array)

   {

   double average;


   average =  sum(array)/ array.length;


   return average;

   }


   public static int max(int[] array)

   {

   int max = array[0];


   for(int i = 1; i < array.length; i++)

   {

       if(array[i] > max)

       {

           max = array[i];

       }

   }


   return max;

   }


   public static int min(int[] array)

   {

   int min = array[0];


   for(int i = 1; i < array.length; i++)

   {

       if(array[i] < min)

       {

           min = array[i];

       }

   }


   return min;

   }


   public static void outputData(int[] array)

   {

   for(int i = 0; i < array.length; i++)

   {

       if(array[i] % 2 == 0)

       {

           PrintWriter outputFile = new PrintWriter("even.out");

       }

       else

       {

           PrintWriter outputFile = new PrintWriter("odd.out");

       }

   }


   outputFile.close();    // error on outputFile

   }

}

Explanation / Answer

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

public class ArrayProcessing
{
public static void main(String[] args)
{
int[] array = inputData();
printArray(array);
reverseArray(array);
System.out.print("The sum of all elements = "+sum(array));
System.out.println();
System.out.print("The average of all elements = "+average(array));
System.out.println();
System.out.print("max = "+max(array));
System.out.println();
System.out.print("min = "+min(array));
System.out.println();
System.out.println();
System.out.println("Done, plese check files 'even.out and 'odd.out'.");
outputData(array);
}
public static int[] inputData()
{
int SIZE=0;
int index = 0;
int[] array=null;
//create a scanner object
Scanner keyboard = new Scanner(System.in);
//get the file name
System.out.println("enter input filename: ");
System.out.println();
String fileName = keyboard.nextLine();
Scanner inputFile=null;
//make sure the file exist
File file = new File(fileName); //error on file
try
{
inputFile = new Scanner(file);
if (!file.exists())
{
System.out.println("The file " + fileName + "is not found.");
System.exit(0);
}
SIZE = inputFile.nextInt();
array = new int[SIZE];
//open the file
//read the array elements to file
while (inputFile.hasNext() && index < array.length)
{
array[index] = inputFile.nextInt();
index++;
}
}
catch(FileNotFoundException IE)
{
System.out.println("The file " + fileName + "is not found.");
}
//close the file
inputFile.close();
return array;
}
public static void printArray(int[] array)
{
System.out.println("original array: ");
for(int r = 0; r < array.length; r++)
{
System.out.print(array[r] + " ");
}
System.out.println();
}
public static void reverseArray(int[] array)
{
int left = 0;
int right = array.length -1;
System.out.println("reversed array:");
while(left < right)
{
int temp = array[left];
array [left] = array[right];
array [right] = temp;
left++;
right--;
}
printArray(array);
}
public static int sum(int[] array)
{
int total = 0;
for(int i = 0; i < array.length; i++)
{
total += array[i];
}
return total;
}
public static double average(int[] array)
{
double average;
average = (double)sum(array)/(double) array.length;
return average;
}
public static int max(int[] array)
{
int max = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] > max)
{
max = array[i];
}
}
return max;
}
public static int min(int[] array)
{
int min = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] < min)
{
min = array[i];
}
}
return min;
}
public static void outputData(int[] array)
{
try
{
PrintWriter even_outputFile = new PrintWriter("even.out");
PrintWriter odd_outputFile = new PrintWriter("odd.out");
for(int i = 0; i < array.length; i++)
{
if(array[i] % 2 == 0)
{
even_outputFile.println(array[i]);
}
else
{
even_outputFile.println(array[i]);
}
}
}
catch(FileNotFoundException FNFE)
{
System.out.println("cant create outputfiles.");
}

//even_outputFile.close(); // error on outputFile
//odd_outputFile.close(); // error on outputFile
}
}