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?

Code :

/*
* 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();
}
}

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.

Re: Adding integers from a file

Quote:

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

Quote:

but now I have to add up the total of the integers. How would I go about doing this?

What integers?

Quote:

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.

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.

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.

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:

Code :

/*
* 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();
}
}

Re: Adding integers from a file

Quote:

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

Quote:

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.

Quote:

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.