Please have a look ?
I debugged it, it works flawlessly.
(though I'm sure there are easier and more efficient ways to do it, just don't know the math functions well enough to use it)
package com.codegym.task.task18.task1820;
/*
Read 2 file names from the console.
The first file contains real (fractional) numbers, separated by spaces. For example, 3.1415.
Round the numbers to integers and write them, separated by spaces, to the second file.
Close the streams.
The rounding should work like this:
3.49 => 3
3.50 => 4
3.51 => 4
-3.49 => -3
-3.50 => -3
-3.51 => -4
Requirements:
1. The program should read a file name twice from the console.
2. Create an input stream for the first file. Create an output stream for the second file.
3. Read the numbers from the first file, round them, and write them to the second file, separated by spaces.
4. The rounding must be performed as indicated in the task.
5. The file streams must be closed.
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String f1 = reader.readLine();
FileInputStream fis = new FileInputStream(f1);
String f2 = reader.readLine();
FileOutputStream fos = new FileOutputStream(f2);
reader.close();
int roundedN = 0;
String numberStr = null;
ArrayList<Float> arrList = new ArrayList<Float>();
byte[] input = new byte[fis.available()];
fis.read(input);
numberStr = new String(input);
for (String s: numberStr.split(" ")){
try{
float fN = Float.parseFloat(s);
arrList.add(fN);
}
catch (Exception e){
System.out.println("eish");
}
}
boolean firstN = true;
for (Float f: arrList){
if (f > 0) {
roundedN = (int)(f + 0.50);
if (firstN){
numberStr = "" + roundedN;
firstN = false;
}
else{
numberStr = " " + roundedN;
}
byte[] b = numberStr.getBytes();
fos.write(b);
}
else{
roundedN = (int)(f - 0.50);
numberStr = " " + roundedN;
byte[] b = numberStr.getBytes();
fos.write(b);
}
}
fis.close();
fos.close();
}
}