Jump to content

Java sum problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
lrobin1

lrobin1

    Newbie

  • Members
  • Pip
  • 3 posts
I'm working on a school project that requires reading buffered input from file, and writing out a count of items, a list of items, and a sum of items. I've got the count, I've got the list, but I can't get a sum of values. I've tried reading and splitting the line, and reading the input to an array, but both return only the value of the last item (last row).
This is the current code of the trouble area:



// Prompt for the input file name


        Scanner console = new Scanner(System.in);

        System.out.print("Input file: ");

        String inputFile = console.next();


// Create variables

        String line = null ;

        File input = new File(inputFile);

        BufferedReader in = new BufferedReader(new FileReader(input));

        double sum = 0;


  while ((line = in.readLine())!= null)     

            {

            String [] plist = line.split("[\\s}]");

	   

               for (int i=0; i < plist.length; i++)

	       {

                sum =+ Double.parseDouble(plist [2]);

               }

            }

            

            System.out.println("Total value of properties listed: " + sum);

            in.close();


This reads the bufferend input file, one line at a time, then should split it into various elements (4 columns of data), then should sum the total of column 3 (plist [2]). Instead my sum equals the value of the item in the last row.

Any insight greatly appreciated!
Thank,

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You have '=+' instead of '+='




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users