Sum Numbers from File

This week we’re going to work with file.

You’re given a file named nums.dat and need to calculate the sum of all integer values in the file.

Sum Numbers in File

For this problem you are given a file named nums.dat that contains integers across multiple lines. There are multiple lines, and there may be 1 or more integers on each line.

The calculateSum method, defined below, should return the sum of all integers in the file nums.dat.

As an example, let's say nums.dat contains the following.

1 3 5 2 122

calculateSum should return 289 because \(\large 1 + 3 + 5 + 2 + 122 + 156 = 289\)

Your task is to implement calculateSum so that it returns the correct value for the entire file.

Why ClassCube?

Are you spending too much time grading your students' source code?

ClassCube is an online tool for Computer Science teachers that allows you to create lab assignments for your students. They'll submit their code online and ClassCube will automatically grade it for you.

More Info …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>