- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFileSum.java
More file actions
Latest commit
43 lines (37 loc) · 866 Bytes
/
Copy pathFileSum.java
File metadata and controls
43 lines (37 loc) · 866 Bytes
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
packagealgorithms;
importjava.io.BufferedReader;
importjava.io.FileReader;
/**
* Prints the sum of numbers in a file (one number per line in the file).
*
* @author joeytawadrous
*/
publicclassFileSum
{
publicstaticvoidmain (String[] args)
{
sumFile("FileSumFile.txt");
}
/**
* Prints the sum of numbers in a file.
* @param name
*/
publicstaticvoidsumFile(Stringname)
{
try
{
inttotal = 0;
BufferedReaderin = newBufferedReader(newFileReader(name));
for(Strings = in.readLine(); s != null; s = in.readLine())
{
total += Integer.parseInt(s);
}
System.out.println(total);
in.close();
}
catch (Exceptione)
{
e.printStackTrace();
}
}
}