-2

So im doing this multidirectional array exercise and have copied from the book. Object Orientated Programming With Java. C thomas Wu page 595.

It makes sense to me the array has two aspects [][], accessed using array[][] etc

In the last part to add 1.50 to each and then display it. Problem is all of a sudden it cant find j when used as an index.

Is the book wrong?

The following is the code.

class PayScaleTable
{
    public static void main (String[]args){
        double [][] payScaleTable = {
        {10.50,12.00,14.50,16.75,18.00},
        {20.50,22.25,24.00,26.25,28.00},
        {34.00,36.50,38.00,40.35,43.00},
        {50.00,60.00,70.00,80.00,99.99}};
    System.out.println("\n");
    System.out.println("\n");     

    //Avg pay of level 2 ppl
    double sum = 0.00, average;
    for (int j=0; j<5;j++){
        sum += payScaleTable[2][j];
    }
    average = sum / 5;
    System.out.println("lvl 2employees " + average);
    System.out.println("\n");
    System.out.println("\n"); 

    //pay difference in grades
    double difference;
    for(int i=0; i<4;i++){
        difference = payScaleTable[i][4] - payScaleTable[i][0];
        System.out.println("pay diff " + i + " is " + difference);
    }

    System.out.println("\n");
    System.out.println("\n"); 
    //print out pay scale table
    for (int i=0; i<payScaleTable.length;i++){
        for(int j=0; j<payScaleTable[i].length; j++){
            System.out.print(payScaleTable[i][j] + " ");
        }
        System.out.print(" ");
    }
    System.out.println("\n");
    System.out.println("\n"); 

    //increase by 1.40 and display results
    for(int i=0;i<payScaleTable[i].length;i++){
        for(int j=0;j<payScaleTable[i].length;j++) 
            payScaleTable[i][j] += 1.50;
            System.out.print(payScaleTable[i][j] + " ");
    }
    System.out.println(" ");
    }
}

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Kyle Harris
  • 137
  • 1
  • 8
  • 3
    This is not JS. I think it's Java. And it seems you need `{` `}` to wrap your loop body. – Oriol Dec 11 '15 at 18:21
  • Yet another reason to use an IDE that does automatic code formatting. You find bugs like this very easily. – azurefrog Dec 11 '15 at 18:26
  • you dont count blueJ as an IDE, surely its better I learn this way via mistakes. Eventually i should get better. – Kyle Harris Dec 11 '15 at 19:10

1 Answers1

4

You are missing braces.

for(int j=0;j<payScaleTable[i].length;j++) 
        payScaleTable[i][j] += 1.50;
        System.out.print(payScaleTable[i][j] + " ");

puts j in scope for the first line inside the loop, but the missing brace means that the loop is only one line long (equivalent to the below):

for(int j=0;j<payScaleTable[i].length;j++) {
        payScaleTable[i][j] += 1.50;
        }
        System.out.print(payScaleTable[i][j] + " ");

Add the braces as follows and it should work:

for(int j=0;j<payScaleTable[i].length;j++) {
    payScaleTable[i][j] += 1.50;
    System.out.print(payScaleTable[i][j] + " ");
}
nanofarad
  • 40,330
  • 4
  • 86
  • 117