Cod sursa(job #1939878)

Utilizator fenrig1Marian fenrig1 Data 26 martie 2017 02:22:13
Problema Text Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 1.28 kb
import java.io.*;

public class Main {

    public static void main(String ...args) {
        String text;
        int totalLetters = 0;
        int totalWords = 0;
        try(BufferedReader reader = new BufferedReader(new FileReader("text.in"))) {
            while((text = reader.readLine()) != null) {
                for(int i = 0; i < text.length();) {
                    int wordCheck = 0;
                    while(i < text.length() && ((text.charAt(i) >= 'a' && text.charAt(i) <= 'z') || (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))) {
                        i++;
                        totalLetters++;
                        wordCheck = 1;
                    }
                    if( wordCheck == 0) {
                        i++;
                    }
                    totalWords += wordCheck;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try(BufferedWriter writer = new BufferedWriter(new FileWriter("text.out"))) {
            writer.write(String.valueOf(totalWords != 0 ? totalLetters/totalWords : 0));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}