1

I need your help! I have an assignment for class where I have to take sales data from an excel list, import it into java, and output a new list that has sorted the Product ID's and the amount of units sold for each product. That means if the original list has 5x Product ID "1003", the final list should only list 1x Product ID "1003" and combine the 5 different unit sales into one total number.

EDIT: I should probably show what the original excel list looks like:

ProductID......Units

10004............. 4

10002............. 2

10004 ............ 3

10008 ............ 6

10009 .............3

etc etc. So basically just combine the duplicate ID's and sum their total units sold.

I'm stuck on two things now. I'm trying to make a new multidimensional array for the final list, but I'm unable to add unique productID's. I tried doing it with a for loop, but it still adds multiples of the productID's.

ALSO, I've tried to sort the final array by the Product ID column in ascending order, but I can't get it to work.

I've put comments with CAPS LOCK to show where the two issues are!

Thanks guys!!!

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;


public class Project1{


public static void main(String[] args) {



Project1 obj = new Project1();
    obj.run();

  }

  public void run() {

    String csvFile = "C:/Users/Jenny/Documents/Classwork/SalesData.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    int counter = 0;
    int salesDataArrayCounter = 0;
    int [][] finalSalesData = new int[200][2];
    System.out.println("Product ID\tUnits");  //Headers for original Data
    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
            String[] salesData = line.split(cvsSplitBy);

            while (counter>0)                //this allows me to skip first line of data (It's just text, I need the numbers)
            {

            System.out.println(salesData[0] + " \t\t " + salesData[1]);
            int productID = Integer.parseInt(salesData[0]);
            int units = Integer.parseInt(salesData[1]);



            search:   //THIS IS WHERE MY FIRST TROUBLE LIES
            {
            for (int i = 0; i < finalSalesData.length; i++)
            {
                if(finalSalesData[i][0]==(productID))       //Should let me find duplicate product ID's and only add the Units to a pre-existing index
                {
                    finalSalesData[i][1] = finalSalesData[i][1] + units;
                    break search;
                }
                else                                        //If productID is unique, add new product ID and units to a new slot in the new array
                {
                    finalSalesData[salesDataArrayCounter][0] = productID;
                    finalSalesData[salesDataArrayCounter][1] = units;
                    salesDataArrayCounter++;
                    break search;
                }
            }
            }

            break;
            }
            counter++;

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("\n\nSorted Sales Data");
    System.out.println("\nProductID\tUnits");

    //HOW DO I SORT THIS DATA IN ASCENDING ORDER OF PRODUCT IDS?

    for (int q = 0; q < finalSalesData.length; q++)
    {
        for (int j = 0; j < finalSalesData[q].length; j++)
        {
            System.out.print(finalSalesData[q][j] +"\t\t");
        }
        System.out.println("");
    }

    System.out.println("Done");
  }

}
  • You could use a sorted `Map` that sorts on the key and then holds the sum of all it's orders as the value. 2 birds - 1 stone. If that's acceptable for your assignment. – ChiefTwoPencils Feb 03 '16 at 19:59

2 Answers2

0

Are you required to use a 2 dimensional array? It might make more sense to make a class that holds the sales information and the product ID.

  1. Make an empty list of product objects.
  2. With each item from the CSV, check the list to see if the product ID is already there. If it is, add the product information to the existing object. If not, add a new product object to your list.

For sorting:

Write a custom comparison method and use the built in sort function as shown here: Sort ArrayList of custom Objects by property

That second while loop in your code would make more sense as an if statement... Or just start your counter at 1.

Community
  • 1
  • 1
Michael
  • 668
  • 5
  • 13
0

I would use a SortedMap to store the results since it sorts ascending on the key by default. This should be defined before the loop:

    SortedMap<Integer,Integer> sortedFinalSalesData = new TreeMap<Integer,Integer>();

After the

int units = Integer.parseInt(salesData[1]);

you can implement something like this:

        Integer currentUnits = sortedFinalSalesData.get(productID);
        sortedFinalSalesData.put(productID, currentUnits != null ? units + currentUnits : units);

Look up tertiary operators if that doesn't make sense since you are supposed to learn something in a school assignment (LOL)!

Andy
  • 209
  • 2
  • 2