java - Add up values from a text file -


Hi guys, I have a text file that contains the amounts on the substrings (34, 47) of each line. I

All values ​​must be added at the end of the file. I have this code that I started to start but I do not know how to proceed from here: public class addup {/ ** * @ ultimate command line argument * / Public Static Zero Main (String [] AGR) file Notought expression, IOException {// TODO code throws 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. Prsd double (number); Println (i); }}}

The output is as follows:

  1.44576457E4 2.33434354E6 4.56875685 E3   

The amount is two In decimal places and I need a result in two decimal places too. What is the best way to achieve this?

decimalfile is the best option to use:

  double round two docurium (double D) {Decimal two defaults = new decimals ("###"); Return double.Whoff (two format formats (D)); }   

You can change your code to:

  Public static zero main (string [] args) FileNotFoundException throws, IOException {// TODO code App logic here double yoga = 0.0; FileInputStream FS = New File InStस्ट ("C: / Analysis / RLL 4004.TXT"); BufferedReader br = new BufferedReader (new InputStreamReader (fs)); String line; While ((line = br.readline ())! = Null {string num = line.substring (34, 47); Double i = double. Prsd double (number); Sum = sum + i; Decimalable two defaults = new decimals ("###); Println (Double.valueOf (twoDForm.format (i))); } System.out.println ("SUM =" + Double.valueOf (twoDForm.format (sum))); }}    

Comments