MarekMatias

VOP13.3

Mar 16th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package com.Marek;
  2.  
  3. import java.io.*;
  4. /**
  5.  * Sum all the floating points in a binary file
  6.  * Suppose a binary data file named  Exercise17_03.dat has been created and its data are created using
  7.  * writeDouble(double) in DataOutputStream. The file contains an unspecified number of floating points
  8.  * Write a program to find the sum of the floating points
  9.  */
  10.  
  11. /**
  12.  * So what to do ?
  13.  * I create a file, Exercise17_03.dat ( not sure if it is correct ), and pars sme floating points.
  14.  * i must input the files content and sum the floating point.
  15.  */
  16.  
  17. public class Main {
  18.  
  19.     public static void main(String[] args) {
  20.         String fileName = "Exercise17_03.dat";
  21.         try {
  22.             try (
  23.                     DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(fileName))
  24.             ) {
  25.                 //Created some input for testing.
  26.  
  27.                 dataOutputStream.writeFloat((float) Math.random() * 10);
  28.                 dataOutputStream.writeFloat((float) Math.random() * 10);
  29.                 dataOutputStream.writeFloat((float) Math.random() * 10);
  30.             }
  31.  
  32.             try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream(fileName))) {
  33.  
  34.                 float theFloatSum = 0;
  35.    
  36.                 while (dataInputStream.available() > 0) {
  37.                     float nextFloat = dataInputStream.readFloat();
  38.                     System.out.println(nextFloat);
  39.                     theFloatSum += nextFloat;
  40.  
  41.  
  42.                 }
  43.                 System.out.println(theFloatSum);
  44.                
  45.             } catch (EOFException e) {
  46.                 System.out.println("End of file was read. ");
  47.             }
  48.  
  49.  
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.  
  54.  
  55.     }
  56. }
Add Comment
Please, Sign In to add comment