0

I'm trying to make an array of integers in java but it won't work for some reason...

I've done the following

int[][] vLines = new int[2][2];

I get issues from Eclipse when I try to initialize my array elements,

  vLines[0][0] = (int)(1*.33);
  vLines[0][1] = (int)(1*.33);
  vLines[1][0] = (int)(1*.33);
  vLines[1][1] = (int)(1*.33);

When I try to store values in my array elements eclipse says "Syntax error, insert '}' to complete block" in the last line of my array init's..

I know there is nothing wrong with my brace balancing in my program...

I thought it might be the cast so I removed '(int)', still doesn't work...

i tried initializing my elements to some integer (e.x. 1), still doesn't work...

I've tried initializing one element, still doesn't work...

I tried splitting my int array declaration to 2 lines and then continuing like this but it still didn't work...

int[][] vLines;
vLines = new int[2][2];

but I still get an error in regards to balancing...

I also tried just copying and pasting java 2D arrays examples from online tutorials and they wont work in eclipse, i've closed and started eclipse and cleaned proj.. still notta

What am I missing? I need my array elements to be dynamically initialized but i can't get my simple 2d array to hold anything...

Here is my whole code file...

import javax.swing.*;
import java.awt.*;

public class ColoredGrid extends JPanel {

    final int ROWS = 2;
    final int COLS = 2;

    int vLines[][] = new int[ROWS][COLS];

    vLines[0][0] = (int)(1*.33);
    vLines[0][1] = (int)(1*.33);
    vLines[1][0] = (int)(1*.33);
    vLines[1][1] = (int)(1*.33);


    public ColoredGrid() {

    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);


    }

}
3
  • You probably have a syntax error elsewhere, since the code you've shown us looks fine. Commented Mar 13, 2012 at 20:28
  • The lines "vLines[0][0] = (int)(1*.33);" etc are NOT allowed in the body of the class. They should either be in an initialization block {} or the constructor. Commented Mar 13, 2012 at 20:36
  • 1
    Incidentally, now that your code works, it's still not actually doing anything useful. (int)(1*.33) is just 0, which is what the elements would be initialized to anyway. Commented Mar 13, 2012 at 21:09

3 Answers 3

3

The lines "vLines[0][0] = (int)(1*.33);" etc are NOT allowed in the body of the class. They should either be in an initialization block {} or the constructor.

{ // init block
  vLines[0][0] = (int)(1*.33);
  vLines[0][1] = (int)(1*.33);
  vLines[1][0] = (int)(1*.33);
  vLines[1][1] = (int)(1*.33);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that the lines where you're assigning values to vLines aren't inside of any function/block, you can't do that. Instead, add those lines to your constructor or wherever you want that initialisation to be done:

public ColoredGrid() {
   vLines[0][0] = (int)(1*.33);
   vLines[0][1] = (int)(1*.33);
   vLines[1][0] = (int)(1*.33);
   vLines[1][1] = (int)(1*.33);
}

An alternative is an initializer block if you don't want that code in the constructor. Just add { } around the four lines to create a block. You might have seen static { ... } before, this is similar but since vLines isn't static you leave out that keyword. Read this page of the Java tutorial for information on initialization.

EDIT: Adam bet me to the initialization blocks.

Comments

0

You are missing a bracket somewhere in your program. It sounds stupid, but go to the end of your program and make sure that the right number of brackets is there at the end. I guarantee thats it.

try commenting out your array code and seeing if the problem persists. if it does, edit your original post to include the entire .java file contents, and ill see if i can fix it for you.

3 Comments

i deleted the array its fine, no bracket issues in the program. I did notice that when I moved my array to my main ftn block it works, no errors, but when i put it in the extended class i'm currently trying to set this array at I get issues...
Not a problem. if you want to post your function code, i can help you get it to work in there, if you need it. also, if this answer works for you, click the up arrow just above my answer to vote it up, and click the check mark just left of that to accept my answer. it gives me rep, which is a nice reward :)
I got it actually!.. when i moved my initialization lines inside my ftn i didn't get an error but when I init in the open part of the class it doesn't work... hmmm.. didn't know this was a rule. But thanks for the help. I read btwn the lines when you asked if I wanted help with my ftn... I'm still working on my ftn, thats why i didn't post anything inside of it! but i got it from here. thanks again!

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.