Hi so i have created this table for a sales person.

class SalesSystem
{
    //Sales Person One
    public void SalesPersonOne()
    {
        int i=0;
        System.out.println("Sales Person 1");
        String newLineMark = System.getProperty("line.separator");

        String leftAlignFormat = "| %-7s | %-9d | %-9d | %-9d | %-9d | %-9d | " + newLineMark;
        System.out.format("+---------+-----------+-----------+-----------+-----------+-----------+" + newLineMark);
        System.out.format("| Day     | Product 1 | Product 2 | Product 3 | Product 4 | Product 5 |" + newLineMark);
        System.out.format("+---------+-----------+-----------+-----------+-----------+-----------+" + newLineMark);
        for (int Day=1; Day<31; Day++)
        {
            System.out.printf(leftAlignFormat,"Day "+ Day, i, i, i, i, i);
        }
      System.out.format ("+---------+-----------+-----------+-----------+-----------+-----------+" + newLineMark);
    }
}

And i want to fill product 1,2,3,4,5 with numbers from an array over the 30 days. How can i do this?

Thanks in advance

Declare a 30x5 array (an array of 30 elements, each of which is a 5 element array of ints).
use your Day variable (line 14) to index the outer array, and hard-code the inner array refs, eg myDataArray[Day][0] instaed of the first "i" on line 16

Ok, i am new to java so i would not know how to do this, would it be on the lines like this:

//Declare array
int MyArray[][] = {{1,2,3,4,5},{6,7,8,9,10}, etc......}

but i would not know how to do the looping, thats even if i done the array right.

Thanks

  1. Yes, that declaration is OK
  2. Looping goes like:

       for (int i = 0; i < myArray.length; i++) {
          for (int j = 0; j < myArray[i].length; j++) {
              do stiff with   myArray[i][j]
          }
       }
    

Edited 3 Years Ago by JamesCherrill

o i know how to the basic looping, i meant how to loop with the code i pasted above on my first code, so it prints everything out in the table format, if that makes sense?

Like I said before, use Day for the outer index, and hard-code the inner indexes, eg

   System.out.printf(leftAlignFormat,"Day "+ Day, myArray[Day][0], myArray{Day][1] etc

ps: Java convention is that variables have names that start with lower-case letters (class names start with a capital)

Awesome thanks for your help, and i will take that advice on board and change my variable names. thanks again

This question has already been answered. Start a new discussion instead.