0

I want to write a method for selecting a specific year (say 2009) from the Time array.

This would refer (8001) which is basically a key for year 2009.

In the FactTable I would like to add all the figures which represent quantity (the 1s). So it would show sales total for year 2009. Anyone have a clue how i can do this.

The Time array stores {timekey, year, month, week, day}

The FactTable stores {clientid, Vehicleid, branchid, timekey, quantity, profit}

 int[][] Time = new int[][] 
        {{8001,2009,1,1,1},
        {8002,2010,1,1,7},
        {8003,2011,1,1,5},
        {8004,2012,1,1,5}};


int[][] FactTable = new int [][]
        {{1,125,20,8001,1,2000},
        {2,126,40,8001,1,1500},
        {3,127,50,8001,1,2500},
        {4,128,10,8001,1,2500}};

int sum = 0; 

int year = 8001;



for (int i = 0; i< FactTable.length-1; i++)
{
    for (int j = 1; j < FactTable[i].length-1; j++)
    {
        year = year + FactTable + [0][4];

    }

}
System.out.println (year); 
4
  • I didn't understand what you want but your syntax has error in year = year + FactTable + [0][4]; ... you couldn't write it [0][4] .. you can write FactTable[0][4] or FactTable[i][j] Commented Dec 19, 2013 at 21:40
  • First, your loop is off by one; use i < FactTable.length and not i < FactTable.length - 1. The latter will skip the last entry. The latter would be correct if you made the condition <= instead of <. Secondly, please consider using a data structure to store this information instead of a multidimensional array. In other words write a Fact class which has ClientId, VehicleId, etc. properties. This will make your code much more readable, maintainable and less error-prone. Commented Dec 19, 2013 at 21:42
  • how about trying "year += FactTable[i][4];" Commented Dec 19, 2013 at 21:42
  • @user3119757 you want a method that returns mapping between YEAR and timeKey ryt?? Commented Dec 19, 2013 at 21:57

5 Answers 5

2

following code shows how to address fields in multidimensional array

int sum = 0; 
int year = 8001;
for (int i = 0; i< FactTable.length; i++)
    {
       if (FactTable[i][3] == year) {
           sum = sum + FactTable[i][4];
       }
    }
System.out.println(sum);

please note correction of second expression in for loop

Sign up to request clarification or add additional context in comments.

Comments

1
public class Test1 {

    public static void main(String[] args) {
        int[][] timeTable = new int[][] { { 8001, 2009, 1, 1, 1 },
                { 8002, 2010, 1, 1, 7 }, { 8003, 2011, 1, 1, 5 },
                { 8004, 2012, 1, 1, 5 } };
        int[][] factTable = new int[][] { { 1, 125, 20, 8001, 1, 2000 },
                { 2, 126, 40, 8001, 1, 1500 }, { 3, 127, 50, 8001, 1, 2500 },
                { 4, 128, 10, 8001, 1, 2500 } };
        meth(factTable, 2009, timeTable);
    }

    private static void meth(int[][] factTable, int year, int[][] timeTable) {
        int timeKey = -1;
        // find the timeKey from the year
        for (int[] is : timeTable) { // for each row in timeTable
            if (is[1] == year) { // if the year column (duh) is the year
                timeKey = is[0]; // get the timekey
                break; // we're done
            }
        }
        if (timeKey == -1) return; //timeKey still -1: no timeKey for year
        int sum = 0;
        for (int[] js : factTable) { // for each row in factTable
            if (js[3] == timeKey) { // if the timeKey column is right
                sum += js[4]; // add to the sum the quantity
            }
        }
        System.out.println(sum); // done
    }
}

Although I really do not get what are those arrays for

10 Comments

what exactly is this doing, when i run this it dont print anything?
@user3119757: Run it again - there was a typo - added some comments
It didnt solve it I was looking for the result to be 4 which is the qantity of sales for 2009.
@user3119757: but in FactTable you have 8001 in all rows
Yeah that means they all purchased it in 2009
|
0

I hope this helps

int year = 2009;
int timeKey = -1;

for (int array[] : Time) {
    if (array[1] == year) {
        timeKey = array[0]; 
        break;
    }
}

int sum = 0;

for (int array[] : FactTable) {
    if (array[3] == timeKey) {
        sum += array[4]; 
    }
}

System.out.println(sum);

1 Comment

@Mr_and_Mrs_D got it first :)
0

You can probably do this: A simplified solution for finding the sum of the values,

int sum=0
for (int i = 0; i< FactTable.length; i++){

         if(FactTable[i][3]==8001){
              sum+=FactTable[i][4]
            }        

}

Comments

0

Array indices are accessed by making a call like so:

Object indexObject = array[index];

You indicate the index, surrounded by square-brackets, immediately following the name of the array.

You code should look like below:

for (int i = 0; i< FactTable.length-1; i++)
{
    for (int j = 1; j < FactTable[i].length-1; j++)
    {
        year = year + FactTable[0][4];  //This is how you access indices of an array.
    }

}

Accessing the indices of multidimensional arrays is not different that accessing the indices of a single-dimension array since the first index of a multidimensional array will return another array with one less dimension. To demonstrate:

Object[][][] array3 = new Object[1][1][1];  //This is a three dimensional array.
Object[][] array2 = array3[0];              //array3[0] returns a two dimensional array.
Object[] array1 = array3[0][0];             //array3[0][0] returns a one dimensional array.

To add together multiple indices of an array, an operation is performed like below:

int a = array[0][4];
int b = array[1][4];
int c = array[2][4];
int d = array[3][4];
int result;

//The two operations below are functionally the same.
result = a + b + c + d;
result = array[0][4] + array[1][4] + array[2][4] + array[3][4];

Aside from the lesson on how to access arrays, JosefN's answer is likely the solution to your problem.

2 Comments

Thanks for your responce, how would i add together 0,4 1,4 2,4 3,4
@user3119757 No thanks necessary. Just be sure to mark JosefN's answer as correct if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.