Question: Can anybody help me as to why my program with the appropiate thrown exceptions only displays arrays are different. import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream;

Can anybody help me as to why my program with the appropiate thrown exceptions only displays arrays are different.

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Random;

public class FileArray {

public static void main(String[] args) {

// TODO Auto-generated method stub

final int SEED = 1000;

final int SIZE = 20;

final String filename = "numbers.dat";

Random rand = new Random(SEED);

double[] array1 = new double[SIZE];

for (int i = 0; i < SIZE; i++) {

array1[i] = rand.nextDouble();

}

writeArray(filename, array1);

double[] array2 = readArray(filename);

if (compareArrays(array1, array2)){

displayArray(array2);

}

else {

System.out.println("Arrays are different");

}

}//main

private static void displayArray(double[] numbers) {

// TODO Auto-generated method stub

for(double number : numbers){

System.out.printf("%,.3f ", number);

}

}

private static boolean compareArrays(double[] firstArray, double[] secondArray) {

// TODO Auto-generated method stub

boolean arraysEqual = true;

int i = 0;

// First determine whether the arrays are the same size.

if (firstArray.length != secondArray.length)

arraysEqual = false;

// Next determine whether the elements contain the same data.

while (arraysEqual && i < firstArray.length)

{

if (firstArray[i] != secondArray[i])

arraysEqual = false;

i++;

}

return arraysEqual;

}

private static double[] readArray(String filename) throws IOException {

// TODO Auto-generated method stub

FileInputStream is = null;

DataInputStream dis = null;

double[] arr = new double[1000];

is = new FileInputStream(filename);

dis = new DataInputStream(is);

// reading double values from the file

int i = 0;

while(dis.available()>0) {

double d = dis.readDouble();

arr[i++] = d;

}

return arr;

}

private static void writeArray(String filename, double[] numbers) {

// TODO Auto-generated method stub

FileOutputStream fstream = null;

try{

fstream = new FileOutputStream(filename);

}

catch (FileNotFoundException e){

System.out.println(e.getMessage());

return;

}

DataOutputStream outputFile = new DataOutputStream(fstream);

for (double number : numbers){

try {

outputFile.writeDouble(number);

}

catch (IOException e) {

System.out.println(e.getMessage());

}

}

try {

outputFile.close();

}

catch (IOException e){

System.out.println(e.getMessage());

}

}

}

This is the required output in java language

0.710 0.575 0.946 0.039 0.486 0.446 0.601 0.550 0.658 0.974 0.630 0.849 0.356 0.136 0.607 0.968 0.958 0.956 0.849 0.097 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!