Hi,
So my thinking was basically this:
get the first file to <Double> list
round and move to <Integer> list
move to <String> list with added " "
change <String> list to String
write to file 2.
I am way top tired to think about more pretty solution (and also eliminate some unecessary steps there), so my question is - is my solution good and the validator just does not want to accept it, or do I have mistake in my solution?
Thanks!
package com.codegym.task.task18.task1820;
/*
Rounding numbers
*/
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1Location = reader.readLine();
String file2Location = reader.readLine();
BufferedReader file1Input = new BufferedReader(new InputStreamReader(new FileInputStream(file1Location), StandardCharsets.UTF_8));
FileOutputStream file2OutPut = new FileOutputStream(file2Location);
String line;
ArrayList<Double> list1 = new ArrayList<>();
while((line=file1Input.readLine()) != null)
{
list1.add(Double.parseDouble(line));
}
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 0; i < list1.size(); i++){
double d = list1.get(i);
int j = (int) Math.round(d);
list2.add(j);
}
ArrayList<String> list3 = new ArrayList<>();
for (int i = 0; i < list2.size(); i++){
String s = list2.get(i).toString();
list3.add(s);
}
String t = "";
for (int i = 0; i < list3.size(); i++){
t = t + list3.get(i) + " ";
}
file2OutPut.write(t.getBytes());
file1Input.close();
file2OutPut.close();
}
}