Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.home05;
- /* Округление чисел
- Считать с консоли 2 имени файла
- Первый файл содержит вещественные(дробные) числа, разделенные пробелом. Например, 3.1415
- Округлить числа до целых и записать через пробел во второй файл
- Закрыть потоки. Не использовать try-with-resources
- Принцип округления:
- 3.49 - 3
- 3.50 - 4
- 3.51 - 4
- -3.49 - -3
- -3.50 - -3
- -3.51 - -4
- */
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- public class Solution {
- public static void main(String[] args) throws IOException{
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String fiename1 = reader.readLine();
- String fiename2 = reader.readLine();
- reader.close();
- /*---------------------------------------------------------------*/
- String arrayString = "";
- for (int i : numbersToList(fiename1)) {
- arrayString += i+" ";
- }
- byte[]bytesArray = arrayString.getBytes();
- FileOutputStream fileOutputStream = new FileOutputStream(fiename2);
- fileOutputStream.write(bytesArray);
- fileOutputStream.close();
- }
- /*---------------------------------------------------*/
- public static int roundThis (double getNumber) {
- if ((Math.abs(getNumber) - Math.abs((int)getNumber))>0.5) {
- if (getNumber>0)
- return (int)Math.ceil(getNumber);
- else {
- return ((int)Math.ceil(getNumber*-1))*-1;
- }
- }
- else {
- if (getNumber > 0)
- return (int)Math.floor(getNumber);
- else
- return ((int)Math.floor(getNumber*-1))*-1;
- }
- }
- /*--------------------------------------------------*/
- static List<Integer> numbersToList (String fileName) throws IOException {
- List <Integer> integers = new ArrayList<Integer>();
- BufferedReader br = new BufferedReader(new FileReader(fileName));
- String sCurrentLine;
- while ((sCurrentLine = br.readLine())!=null) {
- String [] strArr=sCurrentLine.split(" ");
- for (String str : strArr) {
- integers.add(roundThis(Double.parseDouble(str)));
- }
- }
- br.close();
- return integers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement