Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am completley stuck and have been trying to figure this out for days... The pr

ID: 3665182 • Letter: I

Question

I am completley stuck and have been trying to figure this out for days... The program is due by friday -_-

I need to input a .csv file into a my program, once inserted, I need to add up the sums of all the units sold per UNIQUE product ID in the same two-column layout the file started in. (Instead of multiple entries for the same Product Id, just have one with the total units sold)

I can get the Sales Data.csv file to output in the console, but I can't figure out how to organize and sum up the data from there....

HELP ME!

public class Main ( public static void main(String[l args) f String fileName".idea/Sales Data.csv"; File file = new File(fileName); try Scanner inputStream = new Scanner(file); while (inputStream.hasNext)) f String data-inputStream.nextLine); //gets a whole line data.split(", "); System.out.println (data); inputStream.close); catch (FileNotFoundException e) e.printstackTrace); Main /Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home/bin/java Product ID,Units 10002,4 10004,6 10008,2 10010,3 10010,3 10001,10 20003,4 20003,7 30019,1 30020,9 10004,9 10006,7 20005,5 30004,10 20004,2 rminal ® Q: Messages 4:Run 6: TODO ilation completed successfully in 1s 282ms (moments ago)

Explanation / Answer

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class Main {

   /**
   * @param args
   */
   public static void main(String[] args) {
       String fileName = "Sales Data.csv";

       try {
           Map<Integer, Integer> productsMap = new HashMap<Integer, Integer>();
           Scanner scanner = new Scanner(new File(fileName));
           while (scanner.hasNext()) {
               String data = scanner.nextLine();

               String lineArr[] = data.split(",");
               int productID = Integer.parseInt(lineArr[0].trim());
               int units = Integer.parseInt(lineArr[1].trim());
               if (productsMap.get(productID) == null) {
                   productsMap.put(productID, units);
               } else {
                   productsMap.put(productID,
                           units + productsMap.get(productID));
               }

           }
           printMap(productsMap);

       } catch (Exception e) {
           // TODO: handle exception
       }
   }

   /**
   * method to print map
   *
   * @param mp
   */
   public static void printMap(Map mp) {
       System.out.println("ProductID Summation");
       Iterator it = mp.entrySet().iterator();
       while (it.hasNext()) {
           Map.Entry pair = (Map.Entry) it.next();
           System.out.println(pair.getKey() + " " + pair.getValue());
           it.remove(); // avoids a ConcurrentModificationException
       }
   }

}

OUTPUT:

ProductID Summation
10004       9
10001       5
10003       2
10002       3

Input

Note : Sales Data.csv is not downloaded

10002,3
10003,2
10001,3
10004,9
10001,2