Advertisement
jaVer404

level18.lesson10.home05 (tested)

Dec 15th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.home05;
  2.  
  3. /* Округление чисел
  4. Считать с консоли 2 имени файла
  5. Первый файл содержит вещественные(дробные) числа, разделенные пробелом. Например, 3.1415
  6. Округлить числа до целых и записать через пробел во второй файл
  7. Закрыть потоки. Не использовать try-with-resources
  8. Принцип округления:
  9. 3.49 - 3
  10. 3.50 - 4
  11. 3.51 - 4
  12. -3.49 - -3
  13. -3.50 - -3
  14. -3.51 - -4
  15. */
  16.  
  17. import java.io.*;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. public class Solution {
  22.     public static void main(String[] args) throws IOException{
  23.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  24.         String fiename1 = reader.readLine();
  25.         String fiename2 = reader.readLine();
  26.         reader.close();
  27.         fileToList(fiename1);
  28.         byte[]someBytes = listToString(fiename1).getBytes();
  29.         FileOutputStream fileOutputStream = new FileOutputStream(fiename2);
  30.         fileOutputStream.write(someBytes);
  31.         fileOutputStream.close();
  32.     }
  33.     public static int roundThis (double num) {
  34.         return (int)Math.round(num);
  35.     }
  36.  
  37. /*-----------------------------------------------------------*/
  38.     public static List<Integer> fileToList (String fileName) throws IOException{
  39.         List<Integer> listWithInts = new ArrayList<Integer>();
  40.         BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
  41.         String sCurrentLine;
  42.         while ((sCurrentLine=bufferedReader.readLine()) != null) {
  43.             String [] strArg = sCurrentLine.split(" ");
  44.             for (String str : strArg) {
  45.                 listWithInts.add(roundThis(Double.parseDouble(str)));
  46.             }
  47.         }
  48.         bufferedReader.close();
  49.         return listWithInts;
  50.     }
  51. /*--------------------------------------------------------------*/
  52.     public static String listToString (String someFileName) throws IOException{
  53.         String arrayString = "";
  54.         int listSize = fileToList(someFileName).size();
  55.         for (int i = 0; i < fileToList(someFileName).size(); i++) {
  56.             if(i < listSize-1) {
  57.             arrayString += fileToList(someFileName).get(i);
  58.             arrayString+=" ";}
  59.             else
  60.                 arrayString += fileToList(someFileName).get(i);
  61.         }
  62.         return arrayString;
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement