Write a program that reads a file name from the keyboard. The file contains inte
ID: 3561057 • Letter: W
Question
Write a program that reads a file name from the keyboard. 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.
Implement the following methods in the program:
? public static int[] inputData()
Explanation / Answer
import java.io.*;
public class Reader
{
public static int[] inputData()throws IOException
{
BufferedReader inread = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the filename");
String filename = inread.readLine();
try
{
inread = new BufferedReader(new FileReader(filename));
}
catch(IOException e)
{
System.out.println("File not found. Exiting");
System.exit(0);
}
int n = Integer.parseInt(inread.readLine());
int arr[] = new int[n];
int i=0;
String scurrentLine;
while ((scurrentLine = inread.readLine()) != null)
{
arr[i++] = Integer.parseInt(scurrentLine);
}
return arr;
}
public static void reverseArray(int array[])
{
for(int i=0; i<array.length/2; i++)
{
int tmp = array[i];
array[i] = array[array.length-i-1];
array[array.length-i-1] = tmp;
}
}
public static int sum(int array[])
{
int sum = 0;
for(int i=0; i<array.length; i++)
{
sum += array[i];
}
return sum;
}
public static double average(int array[])
{
int sum = sum(array);
return (double)(sum) / (double)(array.length);
}
public static int max(int array[])
{
int max = 0;
for(int i=0; i<array.length; i++)
{
if(max < array[i])
max = array[i];
}
return max;
}
public static int min(int array[])
{
int min = array[0];
for(int i=0; i<array.length; i++)
{
if(min > array[i])
min = array[i];
}
return min;
}
public static void outputData(int array[])
{
try
{
File fileeven = new File("even.out");
File fileodd = new File("odd.out");
// if file doesnt exists, then create it
if (!fileeven.exists())
fileeven.createNewFile();
if (!fileodd.exists())
fileodd.createNewFile();
FileWriter fw = new FileWriter(fileeven.getAbsoluteFile());
BufferedWriter bweven = new BufferedWriter(fw);
fw = new FileWriter(fileodd.getAbsoluteFile());
BufferedWriter bwodd = new BufferedWriter(fw);
for(int i=0; i<array.length; i++)
{
if(array[i]%2 == 0)
bweven.write(array[i]);
else
bwodd.write(array[i]);
}
bweven.close();
bwodd.close();
} catch (IOException e) {
System.out.println("could not write file");
}
}
public static void printArray(int array[])
{
System.out.println("Array contents :");
for(int i=0; i<array.length; i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
}
public static void main(String args[])throws IOException
{
int arr[] = inputData();
printArray(arr);
reverseArray(arr);
printArray(arr);
System.out.println(sum(arr));
System.out.println(average(arr));
System.out.println(max(arr));
System.out.println(min(arr));
outputData(arr);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.