Skip to content

Instantly share code, notes, and snippets.

@josinSbazin
Created July 8, 2016 23:41
Show Gist options
  • Save josinSbazin/e8ee92307f5460c66293304466df39c4 to your computer and use it in GitHub Desktop.
Save josinSbazin/e8ee92307f5460c66293304466df39c4 to your computer and use it in GitHub Desktop.
level18.lesson10.home02
package com.javarush.test.level18.lesson10.home02;
/* Пробелы
В метод main первым параметром приходит имя файла.
Вывести на экран соотношение количества пробелов к количеству всех символов. Например, 10.45
1. Посчитать количество всех символов.
2. Посчитать количество пробелов.
3. Вывести на экран п2/п1*100, округлив до 2 знаков после запятой
4. Закрыть потоки. Не использовать try-with-resources
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Solution {
public static void main(String[] args) throws IOException {
if (args.length == 0) return;
int c;
FileInputStream fis = new FileInputStream(args[0]);
int charCount = fis.available();
int spaceCount = 0;
while (fis.available()>0) {
c = fis.read();
if (c == 32) spaceCount++;
}
fis.close();
double calc = ((double)spaceCount/charCount)*100;
double result = new BigDecimal(calc).setScale(2, RoundingMode.HALF_UP).doubleValue();
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment