1 import java.io.*;
2 import fi.jyu.mit.ohj2.Mjonot;
3
8 public class Tied_ka {
9
10 public static void main(String[] args) {
11
12 BufferedReader fi;
13
14 try { fi = new BufferedReader(new FileReader("luvut.dat"));
16 } catch (FileNotFoundException ex) {
17 System.out.println("Tiedosto ei aukea!");
18 return;
19 }
20
21 double summa=0;
22 int n=0;
23
24 try {
25 String s; double luku;
26 while ( ( s = fi.readLine() ) != null ) {
27 try {
28 luku = Double.parseDouble(s);
29 } catch (NumberFormatException ex) {
30 continue;
31 }
32 summa += luku;
33 n++;
34 }
35 } catch (IOException ex) {
36 System.out.println("Virhe tiedostoa luettaessa!");
37 } finally { try {
39 fi.close(); } catch (IOException ex) {
41 System.out.println("Tiedostoa ei saa suljettua!");
42 }
43 }
44
45 double ka = 0;
46 if ( n > 0 ) ka = summa/n;
47 System.out.println("Lukuja oli " + n + " kappaletta.");
48 System.out.println("Niiden summa oli " + Mjonot.fmt(summa,4,2));
49 System.out.println("ja keskiarvo oli " + Mjonot.fmt(ka,4,2));
50
51 }
52 }
53