So i'm trying to make a module for my program that returns a multidimensional array based on another array in the form of a command-line argument.

Here is the module that's keeping me up at night:

public static double[][][] getDimensions(String [] args)
    {
        final int l = args.length;
        double dimensions [][][] = new double[1][l][l];
        for(int count = 0; count<l; count++)
        {

           if(args[count].equals("NA4"))
           dimensions[count][count][count] = {20.6, 15.7, 12};
            
           if(args[count].equals("NA5"))
           dimensions[count][count][count] = {17.6, 15.7, 12};
            
           if(args[count].equals("NA1"))
           dimensions[count][count][count] = {45, 18.5, 15};
            
           if(args[count].equals("NA2"))
           dimensions[count][count][count] = {32.6, 17, 15};
           
           else
           dimensions[count][count][count] = {14.5, 9.8, 7.1};
        }
        return dimensions;
    }

The error i get is with the consequence of the if statement:
dimensions[count][count][count] = {20.6, 15.7, 12}; - Illegal start of expression
The thing is.. im pretty sure i've forgotten something ridiculously obvious but i'm tired of getting this error.

Your for loop starts count at 0 and says it must be less than 1 so it loops once and count is always 0. next iteration count is 1 and it wont loop as count is not less than 1. edit -added not

the expression:

dimensions[count][count][count]

is read as dimensions[0][0][0] and this is how you map to exactly one element of an array. In fact it looks like your dimensions array can contain only one element. there is no dimensions[1][0][0] for example. that would require an array initialization for the first index of 2.

Mike

Edited 6 Years Ago by adams161: n/a

Thanks for replying so quickly, Mike. Let me explain the for loop, first. It looks like count<1 but it's actually count<l (lowercase L). This is instantiated in the first line of the module and represents the length of the command-line argument args.length. The array on the other hand is used so that dimensions[0][0][0] refer to the width, depth and height of a certain product. That's why i didnt need dimensions[0][0][1] or anything like that. Each 'row' refers to a certain product's dimensions. I'm not quite sure what you mean by an index of 2, but i thought i got the gist of it (or maybe i didnt.. im very new to programming).

The illegal start of expression error still doesnt go away though. It's killing me! Am i creating the arrays incorrectly?

gotcha. probably a bad idea to use L for readability. Maybe bite the bullet and use length :)

the assignment is still wrong.

dimensions[count][count][count]

This is still how you assign a single element.

i.e if count is 5 you have,

dimensions[5][5][5] = ONETHING;

similar to how

myarray[5]=ONETHING;

works

Also i'm not sure that a 3 dimensional array is needed here. Seems you want
dimensions[L][3];

so if there are 5 args and L is 5 you have 5 indexes to use that can store 3 things,

then just do for example:

dimensions[count][0]=num1;
dimensions[count][1]=num2;
dimensions[count][2]=num3;

as to how to do an assignment of dimensions[count] without specifying the 3 indexes, i'm not familiar with that. maybe dimensions[count] = {20.6, 15.7, 12}; would work don't know. You can google ways of assigning values to multi dimensional arrays.

Mike

Hey Mike,

I just tried your solution. Thanks for clearing up my misconceptions about multidimensional arrays. Your code worked like a charm, you've been an enormous help.

Thanks again,
Khodeir

t

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