0

So I have this code :

    public static void main (String[] args) throws IOException
{
    Queue popcorn = new LinkedList();
    BufferedReader in = null;
    int j = 0;
    try {
        File file2 = new File("Events.txt");
        in = new BufferedReader(new FileReader(file2));

        String str;
        String [][] process = new String[][];
        while ((str = in.readLine()) != null) {
            String[] arr = str.split(" ");
            for(int i=0 ; i<str.length() ; i++){
            process[j][i] = in.readLine();
        }
            j++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

} 

It doesn't work. It throws "Variable must provide either dimension expressions or an array initializer"

I am trying to model it after this webpage answer " http://www.chegg.com/homework-help/questions-and-answers/hired-biggy-s-popcorn-handle-popcorn-orders-store-write-java-console-application-reads-dat-q7220573 " which i am PRETTY SURE does not work. Anyway this linked list doesn't seem to be working out. Can someone point me in the right direction as far as my String[][] process declaration is concerned?

2
  • new String[][] - it is not possible to create an array without dimensions, which is what the message says. This is separate from the variable declaration. Search for error messages for general hints/directions. Commented Nov 26, 2016 at 5:51
  • You need to provide array size. Read java67.com/2014/10/… Commented Nov 26, 2016 at 5:53

1 Answer 1

3

You can't just initialize an array with no dimension parameters. For example, this is invalid:

int[] array = new int[];

It needs to be initialized like this:

int[] array = new int[10];

Or simply:

int[] array;
// ... //
array = new int[10];

It's the same thing with multi-dimensional arrays. To make an array containing 3 arrays of size 5, you would put:

int[][] array = new int[3][5];

However, with 2D arrays, you may also put:

int[][] array = new int[3][];
array[0] = new int[5];
array[1] = new int[7];
// ... //

The point is, you need to define how many other arrays will be in your base array, and may also optionally define the size of all of the arrays or simply add them later. In this case, you'll need to change

String [][] process = new String[][];

to something like

String [][] process = new String[x][y];
Sign up to request clarification or add additional context in comments.

2 Comments

Alright! So I will use String [][] process = new String[15][]; instead right?
Yes, but keep in mind that you will need to populate the base array with other arrays if you want to use the line process[j][i] = in.readLine(); , which might be able to be done by setting process[j] = arr; .

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.