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