Results 1 to 9 of 9
  1. #1
    Cubba27 is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Exclamation 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();

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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:
    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
    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-

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's why i told,

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

    So he can learn about arrays.

  5. #5
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    just change

    Java Code:
    int result = in.nextInt();
    for

    Java Code:
    total += in.nextInt();

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by fceruti View Post
    just change

    Java Code:
    int result = in.nextInt();
    for

    Java Code:
    total += in.nextInt();
    No. That won't work.

    -Gary-

  7. #7
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    true you need the result value to print it, so keep that line and add below:

    Java Code:
    total += result;
    that works

  8. #8
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    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

  9. #9
    Cubba27 is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    Java 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 :)

    STRING allPostersOnThread = "HEROES!!!!!!"; :D

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 06:45 PM
  2. Sorting 3 Integers Using If Else
    By MSteinman in forum New To Java
    Replies: 12
    Last Post: 02-19-2010, 01:52 PM
  3. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 11:29 AM
  4. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 04:44 PM
  5. How to write integers into file?
    By dj kourampies in forum New To Java
    Replies: 1
    Last Post: 08-20-2009, 05:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •