-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileSum.java
43 lines (37 loc) · 866 Bytes
/
FileSum.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
32
33
34
35
36
37
38
39
40
41
42
43
package algorithms;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* Prints the sum of numbers in a file (one number per line in the file).
*
* @author joeytawadrous
*/
public class FileSum
{
public static void main (String[] args)
{
sumFile("FileSumFile.txt");
}
/**
* Prints the sum of numbers in a file.
* @param name
*/
public static void sumFile(String name)
{
try
{
int total = 0;
BufferedReader in = new BufferedReader(new FileReader(name));
for(String s = in.readLine(); s != null; s = in.readLine())
{
total += Integer.parseInt(s);
}
System.out.println(total);
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}