Pagini recente » Cod sursa (job #2650854) | Cod sursa (job #767803) | Cod sursa (job #1919350) | Istoria paginii utilizator/eva1.618 | Cod sursa (job #1939878)
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();
}
}
}