I know it works, even with Integer, I tested it on custom files I created.
It's two times in a row that I have the good output but CodeGym doesn't accept my solution...
package com.codegym.task.task18.task1820;
/*
Rounding numbers
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = null;
String file2 = null;
String numberString = null;
int roundedNumber = 0;
ArrayList<Float> numbers = new ArrayList<>();
file1 = reader.readLine();
file2 = reader.readLine();
FileInputStream fileInputStream = new FileInputStream(file1);
FileOutputStream fileOutputStream = new FileOutputStream(file2, true);
byte[] input = new byte[fileInputStream.available()];
fileInputStream.read(input);
numberString = new String(input);
for (String retval: numberString.split(" ")) {
float data = Float.parseFloat(retval);
numbers.add(data);
}
for (Float f : numbers) {
if ( f >= 0) {
roundedNumber = (int) (f + 0.5);
String s = roundedNumber + " ";
byte[] b = s.getBytes();
fileOutputStream.write(b);
}
else {
roundedNumber = (int) (f - 0.49);
String s = roundedNumber + " ";
byte[] b = s.getBytes();
fileOutputStream.write(b);
}
}
FileChannel fileChannel = new FileOutputStream(file2, true).getChannel();
fileChannel.truncate(fileChannel.size() - 1); //Removes last character
fileChannel.close();
reader.close();
fileInputStream.close();
fileOutputStream.close();
}
}