Created
July 8, 2016 23:41
-
-
Save josinSbazin/e8ee92307f5460c66293304466df39c4 to your computer and use it in GitHub Desktop.
level18.lesson10.home02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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