-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReadLinesFromFile.java
31 lines (27 loc) · 1.15 KB
/
ReadLinesFromFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.*;
public class ReadLinesFromFile {
public static void main(String a[]){
BufferedReader br = null;
String strLine = "";
int sum = 0 , num =0, count = 0 ,ave = 0;
try {
br = new BufferedReader( new FileReader("numbers.txt"));
while( (strLine = br.readLine()) != null){
System.out.println(strLine);
num = Integer.parseInt(strLine); // convert string to int
sum += num; // add all element in the text file
count++; // count all element in the text file
}
ave = sum / count;
System.out.println("The sum is = " + sum);
System.out.println("The number of element is = " + count);
System.out.println("The average of the numbers is = " + ave);
} catch (FileNotFoundException e) {
System.err.println("Unable to find the file: fileName");
} catch (IOException e) {
System.err.println("Unable to read the file: fileName");
} catch( Exception e){
System.err.println("Any Exception");
}
}
}