How do I get sum LINE BY LINE only????


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How do I get sum LINE BY LINE only????

  1. #1
    Join Date
    Sep 2003
    Posts
    1

    How do I get sum LINE BY LINE only????

    Hi Everyone! I'm pretty new here and to java programming as well.

    I was wondering if u guys could help me out. I got this program below... it compiles and runs.
    The problem it adds all the numbers together and than returns the total value, what I would like it to do is get the sum of the numbers on each line only. So instead of getting one number (which is what I am getting) how do I get the sum of each line?

    import java.io.*;
    import java.util.*;

    class FileTest
    {
    public static void main (String[] args)
    {
    Controller contr = new Controller();
    System.out.println (contr.getSum());
    }
    }

    class Controller
    {
    FileAccess fa = new FileAccess();
    public int getSum()
    {
    int lineNum = 1;
    int sum = 0;
    String lineOfText;

    fa.openFile("Kill.txt");
    lineOfText = fa.readLine();

    while (lineOfText != null)
    {
    System.out.println (lineNum);
    StringTokenizer strToken = new StringTokenizer (lineOfText, ";");

    while (strToken.hasMoreTokens())
    {
    sum += Integer.parseInt (strToken.nextToken());
    }

    lineNum++;
    lineOfText = fa.readLine();
    }

    return sum;
    }
    }

    class FileAccess
    {
    private BufferedReader br;

    public void openFile (String fileName){
    try
    {
    br = new BufferedReader (new FileReader (fileName));
    }

    catch (FileNotFoundException fnfe)
    {
    System.out.println ("File does not exist.");
    }
    }

    public void closeFile()
    {
    try
    {
    br.close();
    }

    catch (IOException ioe)
    {
    System.out.println ("There was an input/output error");
    }
    }

    public String readLine()
    {
    String lineOfText = null;

    try
    {
    lineOfText = br.readLine();
    }

    catch (IOException ioe)
    {
    System.out.println ("There was an input/output error");
    }

    return lineOfText;
    }
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    This looks an awful lot like a "modify this program to..." homework, so I'm not going to just give you the solution - it won't benefit you in the long run.

    However, it's not very difficult to modify the getSum() method to return the sum of each line (about four lines of code I think).

    How do *you* think it should be done? Once you've answered that question, we'll start talking specifics.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center