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");
}
}