Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Adding integers from a file

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Adding integers from a file

    My program works effectively, but now I have to add up the total of the integers. How would I go about doing this? The program is just reading a simple file in c:/ which it does, and displays the files text, which it does. How do i add up the sum of these numbers?

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package readfile;
     
    import java.io.*;
    public class ReadFile {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException, FileNotFoundException {
            processFile("c:/scores.txt");
        }
        public static void processFile (String scores) throws IOException, FileNotFoundException {
     
            FileInputStream fstream = new FileInputStream("c:/scores.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) !=null) {
                System.out.println(strLine);
            }
            //Closing input stream
            in.close();
     
     
     
     
     
        }
     
    }


  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding integers from a file

    Any help would be appreciated. I added a parse to double, so now the string that is read from the file is converted to a double.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Adding integers from a file

    My program works effectively,....The program is just reading a simple file in c:/ which it does, and displays the files text, which it does.
    ok...
    but now I have to add up the total of the integers. How would I go about doing this?
    What integers?
    I added a parse to double, so now the string that is read from the file is converted to a double
    If you are adding integers there is no reason to use double. Use int.

    To you, you may have provided plenty of information. To people who have no clue what your project is supposed to do, there is much missing...
    I am guessing you are reading some type of numbers from a text file and want to get a total of all of the numbers? Anything in the file besides numbers? Are the numbers all integers or what are they?
    Is it one number per line? If it is for an assignment it may be helpful to post the instructions. If not try to explain what the contents of the file are supposed to be, and exactly what you are supposed to do.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding integers from a file

    In my textfile, my goal is to add up the sum of all the integers, the text file is in a formal like this;
    15
    14
    23
    18
    74
    38
    One per line. That is the only thing in the textfile, integers. I am stuck on how to add up the sum of all these integers that are on their own line.
    So far, I have only been able to get the numbers to display.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Adding integers from a file

    Lets pretend you are standing in front of me, and I say to you, "Hey add these numbers up" and I start giving you numbers...

    How would you add them up?
    I say 15.
    You say 15.
    I say 14.
    You say 29.
    I say 23.
    You say 52.
    I say done.
    You say the total is 52.
    Every time I give you a new number, you add it to a running total. That would be one way.

    Another way.
    I say 15.
    You say 15.
    I say 14.
    You say 15, 14.
    I say 23.
    You say 15, 14, 23.
    I say done.
    You say the total is 52.
    Every time I give you a number, you remember every number until the end, and then add them all up.

    The first way may be preferable if you only need a total. The second way also works, but may be more preferable in other situations, or possibly this one. Decide on how you will solve the problem first, and worry about writing code to perform the steps of the solution later.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding integers from a file

    I could do that, but I do not know how to read each line seperately.
    Update: I was able to add up all of the integers, and display what it equals, but now how do i get the mean or avg of these?
    For some reason, it is printing out the mean of each seperate integer, but it want it only to calculate once with the total of all integers

    Code so far:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package readfile;
     
    import java.io.*;
    public class ReadFile {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException, FileNotFoundException {
            processFile("c:/scores.txt");
     
        }
        public static void processFile (String scores) throws IOException, FileNotFoundException {
     
            FileInputStream fstream = new FileInputStream("c:/scores.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            double doubleValue;
            int accumulator = 0;
            while ((strLine = br.readLine()) !=null) {
                doubleValue = Integer.parseInt(strLine);
                System.out.println(doubleValue);
                accumulator += (doubleValue);
                System.out.print ("Total Sum:  ");
                System.out.println(accumulator);
                System.out.print("Mean of Scores:  ");
                System.out.println(accumulator/20);
     
     
     
     
            }
            //Closing input stream
            in.close();
     
     
     
     
     
        }
     
    }
    Last edited by LoganC; November 1st, 2012 at 03:25 PM.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Adding integers from a file

    I could do that, but I do not know how to read each line seperately.
    Sure you do. The following statement in your posted code does just that:
    strLine = br.readLine()

    I was able to add up all of the integers, and display what it equals, but now how do i get the mean or avg of these?
    Did you read what I said near the bottom of my other post? Pay more attention to the second example or think of a third way. To get an average you will need to know how many numbers were added together right? Going to have to count how many numbers you add up then... Figure out a way to do that.

    For some reason, it is printing out the mean of each seperate integer, but it want it only to calculate once with the total of all integers
    The computer just does what the code tells it to do when it is told to do it. If you find something happening to every number as it goes instead of one time at the end, consider where the line of code is placed in terms of when and how many times it will run.

Similar Threads

  1. Replies: 6
    Last Post: June 18th, 2012, 04:54 AM
  2. [SOLVED] Reading integers from a file, but it's only displaying the first number repeatedly..?
    By jessica202 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2011, 08:16 AM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM