Results 1 to 9 of 9
Thread: Sum of Integers - From File
- 03-25-2010, 02:41 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
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:
Java 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, 02:59 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 03:38 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
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.Java 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
-Gary-
- 03-25-2010, 03:41 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's why i told,
:pJust think about that... you have lots of difficulties.
So he can learn about arrays.
- 03-25-2010, 07:10 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
just change
forJava Code:int result = in.nextInt();
Java Code:total += in.nextInt();
- 03-25-2010, 07:12 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 03-25-2010, 07:19 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
true you need the result value to print it, so keep that line and add below:
that worksJava Code:total += result;
- 03-25-2010, 07:20 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
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, 11:45 AM #9
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Reading Integers from a text file
By tress in forum New To JavaReplies: 6Last Post: 02-26-2011, 05:45 PM -
Sorting 3 Integers Using If Else
By MSteinman in forum New To JavaReplies: 12Last Post: 02-19-2010, 12:52 PM -
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01-28-2010, 10:29 AM -
file input: array of integers
By hannes in forum New To JavaReplies: 8Last Post: 01-27-2010, 03:44 PM -
How to write integers into file?
By dj kourampies in forum New To JavaReplies: 1Last Post: 08-20-2009, 04:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks