Add up values from a text file

Posted by Stanley on Stack Overflow See other posts from Stack Overflow or by Stanley
Published on 2012-07-10T09:08:55Z Indexed on 2012/07/10 9:15 UTC
Read the original article Hit count: 208

Filed under:
|
|
|

Hi Guys I have a text file that contains Amounts at Substring (34, 47) of each line. I need to sum Up all the Values to the End of the File. I have this code that I had started to build but I do not know how to proceed from here:

public class Addup {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO code application logic here
    FileInputStream fs = new FileInputStream("C:/Analysis/RL004.TXT");
    BufferedReader br = new BufferedReader(new InputStreamReader(fs));
    String line;
    while((line = br.readLine()) != null){
        String num = line.substring(34, 47);

        double i = Double.parseDouble(num);
        System.out.println(i);
    }
}
}

The output is like this:

1.44576457E4
2.33434354E6
4.56875685E3

The Amount is in two decimal Places and I need the result also in the Two decimal Places. What Is the Best way to achieve this?

© Stack Overflow or respective owner

Related posts about java

Related posts about file-io