Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

array problem

Thread view: 
Ed - 12 Jun 2006 18:05 GMT
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);
Peter Van Weert - 12 Jun 2006 18:19 GMT
> 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
Ed - 12 Jun 2006 19:08 GMT
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.

>> Can someone tell me what I'm doing wrong or point me to help towards a
>> solution. Thanks.
[quoted text clipped - 23 lines]
> Cheers,
> Peter
Oliver Wong - 12 Jun 2006 19:16 GMT
> 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
Lee Fesperman - 12 Jun 2006 21:50 GMT
> Using Java 5.0
> I'm trying to prepare a DefaultStyledDocument array so that I can then
[quoted text clipped - 24 lines]
>   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),
     };

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Peter Van Weert - 12 Jun 2006 22:23 GMT
Oops, can't believe I missed that one :-)
Well spotted Lee!

Lee Fesperman schreef:
>> Using Java 5.0
>> I'm trying to prepare a DefaultStyledDocument array so that I can then
[quoted text clipped - 50 lines]
>         new DefaultStyledDocument(sc),
>       };
Ed - 13 Jun 2006 14:08 GMT
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.

> Oops, can't believe I missed that one :-)
> Well spotted Lee!
[quoted text clipped - 56 lines]
>>         new DefaultStyledDocument(sc),
>>       };


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.