array problem

E

Ed

Using Java 5.0
I'm trying to prepare a DefaultStyledDocument array so that I can then
insert formatted text into each element. My compiler complains about my
initializing code. Wherever each element of the array is referenced '[0]' it
says a closing square bracket "]" is expected instead of a digit.

Can someone tell me what I'm doing wrong or point me to help towards a
solution. Thanks.

A part of my code follows:

class PublishStats extends JFrame
{
JFrame f = new JFrame("BJ");
StyleContext sc = new StyleContext();

//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate and array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

//Initialize each element of array "doc"
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
 
P

Peter Van Weert

Can someone tell me what I'm doing wrong or point me to help towards a
solution. Thanks.
//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate and array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

You have declared the variable doc twice. The following is correct:

//Declare and instantiate an array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

or also correct:

//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate the array "doc"
doc = new DefaultStyledDocument[5];

I think (the error message of the compiler is a bit puzzling) this will
solve your problem.

Cheers,
Peter
 
E

Ed

Thanks. Peter - I understood that in addition to declaring the array I had
to initialize each element also. In an earlier version of my code when I had
only the code you suggest the compiler objected, saying that the array "may
not be initialized" That is why I added the five lines referring to each
element.


Peter Van Weert said:
Can someone tell me what I'm doing wrong or point me to help towards a
solution. Thanks.
//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate and array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

You have declared the variable doc twice. The following is correct:

//Declare and instantiate an array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

or also correct:

//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate the array "doc"
doc = new DefaultStyledDocument[5];

I think (the error message of the compiler is a bit puzzling) this will
solve your problem.

Cheers,
Peter
 
O

Oliver Wong

Ed said:
Thanks. Peter - I understood that in addition to declaring the array I had
to initialize each element also. In an earlier version of my code when I
had only the code you suggest the compiler objected, saying that the array
"may not be initialized" That is why I added the five lines referring to
each element.

That's not the chance that Peter is talking about. Given explicitly,
here's what your code should look like:

<code>
class PublishStats extends JFrame
{
JFrame f = new JFrame("BJ");
StyleContext sc = new StyleContext();

//Declare an array "doc" and
//Instantiate it
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

//Initialize each element of array "doc"
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
</code>

- Oliver
 
L

Lee Fesperman

Ed said:
Using Java 5.0
I'm trying to prepare a DefaultStyledDocument array so that I can then
insert formatted text into each element. My compiler complains about my
initializing code. Wherever each element of the array is referenced '[0]' it
says a closing square bracket "]" is expected instead of a digit.

Can someone tell me what I'm doing wrong or point me to help towards a
solution. Thanks.

A part of my code follows:

class PublishStats extends JFrame
{
JFrame f = new JFrame("BJ");
StyleContext sc = new StyleContext();

//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate and array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

//Initialize each element of array "doc"
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);

Besides the double declaration of doc that Peter mentioned, you're also placing the
individual assignments incorrectly, causing the compiler error. Assignment statements
(versus declaration statements) must be placed inside a method or an initializer. You
can specify an instance initializer by placing braces around the last sequence ...

//Initialize each element of array "doc"
{
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
}

.... or in a constructor, or you could do it in the declaration, like so:

DefaultStyledDocument[] doc =
{
//Initialize each element of array "doc"
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
};
 
P

Peter Van Weert

Oops, can't believe I missed that one :)
Well spotted Lee!

Lee Fesperman schreef:
Ed said:
Using Java 5.0
I'm trying to prepare a DefaultStyledDocument array so that I can then
insert formatted text into each element. My compiler complains about my
initializing code. Wherever each element of the array is referenced '[0]' it
says a closing square bracket "]" is expected instead of a digit.

Can someone tell me what I'm doing wrong or point me to help towards a
solution. Thanks.

A part of my code follows:

class PublishStats extends JFrame
{
JFrame f = new JFrame("BJ");
StyleContext sc = new StyleContext();

//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate and array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

//Initialize each element of array "doc"
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);

Besides the double declaration of doc that Peter mentioned, you're also placing the
individual assignments incorrectly, causing the compiler error. Assignment statements
(versus declaration statements) must be placed inside a method or an initializer. You
can specify an instance initializer by placing braces around the last sequence ...

//Initialize each element of array "doc"
{
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
}

... or in a constructor, or you could do it in the declaration, like so:

DefaultStyledDocument[] doc =
{
//Initialize each element of array "doc"
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
};
 
E

Ed

Either putting it inside of curly brackets or in a method works. That was
the problem all along. Thanks to all - you are very helpful.


Peter Van Weert said:
Oops, can't believe I missed that one :)
Well spotted Lee!

Lee Fesperman schreef:
Ed said:
Using Java 5.0
I'm trying to prepare a DefaultStyledDocument array so that I can then
insert formatted text into each element. My compiler complains about my
initializing code. Wherever each element of the array is referenced
'[0]' it
says a closing square bracket "]" is expected instead of a digit.

Can someone tell me what I'm doing wrong or point me to help towards a
solution. Thanks.

A part of my code follows:

class PublishStats extends JFrame
{
JFrame f = new JFrame("BJ");
StyleContext sc = new StyleContext();

//Declare an array "doc"
DefaultStyledDocument[] doc;

//Instantiate and array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];

//Initialize each element of array "doc"
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);

Besides the double declaration of doc that Peter mentioned, you're also
placing the individual assignments incorrectly, causing the compiler
error. Assignment statements (versus declaration statements) must be
placed inside a method or an initializer. You can specify an instance
initializer by placing braces around the last sequence ...

//Initialize each element of array "doc"
{
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
}

... or in a constructor, or you could do it in the declaration, like so:

DefaultStyledDocument[] doc =
{
//Initialize each element of array "doc"
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top