MarekMatias icon

VOP13.3

MarekMatias | PRO | 03/16/17 07:43:29 PM UTC | 0 ⭐ | 177 👁️ | Never ⏰ | []
Java |

1.77 KB

|

None

|

0 👍

/

0 👎

package com.Marek;
 
import java.io.*;
/**
 * Sum all the floating points in a binary file
 * Suppose a binary data file named  Exercise17_03.dat has been created and its data are created using
 * writeDouble(double) in DataOutputStream. The file contains an unspecified number of floating points
 * Write a program to find the sum of the floating points
 */
 
/**
 * So what to do ?
 * I create a file, Exercise17_03.dat ( not sure if it is correct ), and pars sme floating points.
 * i must input the files content and sum the floating point.
 */
 
public class Main {
 
    public static void main(String[] args) {
        String fileName = "Exercise17_03.dat";
        try {
            try (
                    DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(fileName))
            ) {
                //Created some input for testing. 
 
                dataOutputStream.writeFloat((float) Math.random() * 10);
                dataOutputStream.writeFloat((float) Math.random() * 10);
                dataOutputStream.writeFloat((float) Math.random() * 10);
            }
 
            try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream(fileName))) {
 
                float theFloatSum = 0;
    
                while (dataInputStream.available() > 0) {
                    float nextFloat = dataInputStream.readFloat();
                    System.out.println(nextFloat);
                    theFloatSum += nextFloat;
 
 
                }
                System.out.println(theFloatSum);
                
            } catch (EOFException e) {
                System.out.println("End of file was read. ");
            }
 
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
 
    }
}

Comments