# Sum of Integers - From File

• 03-25-2010, 03:41 AM
Cubba27
Sum of Integers - From File
Hey All,

right i've been tasked with writing a program that reads data from a file, consisting of both types String & Int, and then writing that data to another file with the addition of the integers totaled up. I've managed to get the majority of the program working, however i stuck at how to total up the Integers. Below is the input file and the expected output:

Input File:

Exam1 66
Exam2 57
Exam3 40
Exam4 72

Output File:

Exam1 66
Exam2 57
Exam3 40
Exam4 72
Total Marks: 235

Could anyone help me to figure this one out please, i've been at it for a fair few days now. Thanks :)

below is part of current code:

Code:

```        int total = 0;         File input = new File("input.txt");         File output = new File("output.txt");         Scanner in = new Scanner(input);                   PrintWriter writeOutput = new PrintWriter(output);                 while (in.hasNext())             {                 String exam = in.next();                 int result = in.nextInt();                 writeOutput.println(exam+" "+result);               }                 writeOutput.print("Total: "+total);                 writeOutput.close();```
• 03-25-2010, 03:59 AM
Eranga
Store all in integers in an arrays, and add up them before write to the second file. Just think about that... you have lots of difficulties. Think about it first.
• 03-25-2010, 04:38 AM
gcalvin
Eranga's suggestion is a good one, but based on the requirements you have stated, an array is not even necessary. Here's what you need to do:
Code:

```    open a file for reading     open a file for writing     initialize sum to 0     while there is still more to read         read a line from the first file         extract the integer from the line         add the integer to sum         write the line on the second file     write your Total Marks line on the second file     close the second file     close the first file```
Some of those things are easy to do, and others are slightly harder. Your code is not far off, but you are forgetting one simple but crucially important part. Also, have you learned about BufferedReader and BufferedWriter? If so, I would use them for this exercise.

-Gary-
• 03-25-2010, 04:41 AM
Eranga
That's why i told,

Quote:

Just think about that... you have lots of difficulties.
:p

So he can learn about arrays.
• 03-25-2010, 08:10 AM
fceruti
just change

Code:

`int result = in.nextInt();`
for

Code:

`total += in.nextInt();`
• 03-25-2010, 08:12 AM
gcalvin
Quote:

Originally Posted by fceruti
just change

Code:

`int result = in.nextInt();`
for

Code:

`total += in.nextInt();`

No. That won't work.

-Gary-
• 03-25-2010, 08:19 AM
fceruti
true you need the result value to print it, so keep that line and add below:

Code:

`total += result;`
that works
• 03-25-2010, 08:20 AM
fceruti
oh and btw, every time you do i/o operations you need to put try/catch statements. Fix that 2 things and you have ur code running
• 03-25-2010, 12:45 PM
Cubba27
Code:

`total += in.nextInt();`
Ah HaHa... thank you all :). I had in fact already used the code above when trying to get it working, but after reading all of your suggestions i got it.... can't believe how close i was :)