readFile Reads from a file and returns a ragged array of dou

readFile

Reads from a file and returns a ragged array of doubles The maximum rows is 10 and the maximum columns for each row is 10 Each row in the file is separated by a new line Each element in the row is separated by a space Suggestion: You need to know how many rows and how many columns there are for each row to create a ragged array. 1. Read the doubles from the file into an a temporary array [10][10] of Strings which was initialized to nulls. 2. Find out how many rows there are (any row that has the first element != null is a valid row) 3. Create the array based on the num of rows, i.e. double[][]array = new double[#rows][] 4. Determine the number of columns for the first row (any element != null is a valid element) 5. Create the first row, i.e. array[0] = new double[#columns] 6. Put the values from the temporary array into in the row (don\'t forget to convert from strings to doubles) 7. Repeat for all rows

Parameters:

file - the file to read from

Returns:

a two dimensional ragged (depending on data) array of doubles if the file is not empty, returns a null if file is empty

Throws:

java.io.FileNotFoundException

writeToFile

Writes the ragged array of doubles into the file. Each row is on a separate line within the file and each double is separated by a space.

Parameters:

data - two dimensional ragged array of doubles

outputFile - the file to write to

Throws:

java.io.FileNotFoundException - if outputFile is not valid

here are the txt file contents please hel and explain

1253.65 4566.50 2154.36 7532.45 3388.44 6598.23
2876.22 3576.24 1954.66
4896.23 2855.29 2386.36 5499.29
2256.76 3623.76 4286.29 5438.48 3794.43
3184.38 3654.65 3455.76 6387.23 4265.77 4592.45
2657.46 3265.34 2256.38 8935.26 5287.34

thank you

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

public static double[][] readFile(java.io.File file) throws IOException{

      

       // creating 2-D string array

       String[][] strArr = new String[10][10];

      

       // creating file reader

       FileReader fr = new FileReader(file);

       BufferedReader br = new BufferedReader(fr);

      

       String line;

       int row= 0;

       while((line = br.readLine()) != null){

           //splitting by space

           String currStrArr[] = line.split(\"\\\\s+\");

           strArr[row++] = currStrArr;

       }

      

       // finding number of row

       System.out.println(\"Number of rows: \"+ row);

      

       // creating 2-D array

       double[][] arr = new double[row][];

      

       // storing each row od string array in arr

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

          

           int column = strArr[i].length;

           // creating 1-D double array

           double currArr[] = new double[column];

          

           // converting each element of current row in double and strong in currArr

           for(int j=0; j<column; j++){

               currArr[j] = Double.parseDouble(strArr[i][j]);

           }

          

           // stroring current double row in arr

           arr[i] = currArr;

       }

      

       br.close();

       fr.close();

      

       return arr;

      

   }

  

   public static void writeToFile(double[][] data,java.io.File outputFile) throws IOException{

      

       // opening File Writer

       FileWriter fw = new FileWriter(outputFile);

       BufferedWriter bw = new BufferedWriter(fw);

      

       // writing in file

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

           String line = \"\";

           for(int j=0; j<data[i].length; j++){

               line = line + data[i][j]+\" \";

           }

          

           bw.write(line);

           bw.newLine();

       }

      

       bw.close();

       fw.close();

      

   }

readFile Reads from a file and returns a ragged array of doubles The maximum rows is 10 and the maximum columns for each row is 10 Each row in the file is separ
readFile Reads from a file and returns a ragged array of doubles The maximum rows is 10 and the maximum columns for each row is 10 Each row in the file is separ
readFile Reads from a file and returns a ragged array of doubles The maximum rows is 10 and the maximum columns for each row is 10 Each row in the file is separ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site