 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eduardo Bernal Guest
|
Posted: Wed Nov 24, 2004 5:04 pm Post subject: NEWBIE HELP: Missing identifier? |
|
|
why does this work:
String[] strArray = new String[10];
and this does not:
String[] strArray;
strArray = new String[]
I've tried this with different IDEs (drjava, netBeans, and JCreator)
and they all complain aobut a missing identifier or
"identifier expected".
It looks like the example at the Sun java tutortial site.
TIA
Ed
--
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
"Turns out that an American with sunglasses and listening to rock music,
naming his tank 'Anger Management' is a much, much deadlier guy than
a suicide bomber." - Victor Davis Hanson
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Wed Nov 24, 2004 5:11 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
On Wed, 24 Nov 2004 17:04:14 +0000 (UTC), Eduardo Bernal wrote:
...
| Quote: | String[] strArray;
strArray = new String[]
|
// must end each statement with ';'
strArray = new String[];
| Quote: | I've tried this with different IDEs (drjava, netBeans, and JCreator)
|
You should try working the problem instead. You need to find out
what causes the error, and how to fix it, because your IDE won't.
| Quote: | and they all complain
|
No they don't. They merely report what the Java compiler
is telling them.
| Quote: | .aobut a missing identifier or
"identifier expected".
|
First place to check errors..
<http://mindprod.com/jgloss/compileerrormessages.html>
<http://mindprod.com/jgloss/runerrormessages.html>
| Quote: | It looks like the example at the Sun java tutortial site.
|
What example? URL?
--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
|
|
| Back to top |
|
 |
Eduardo Bernal Guest
|
Posted: Wed Nov 24, 2004 5:51 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
On Wed, 24 Nov 2004 17:11:15 GMT, The Trilateral Commission's orbital mind control lasers transmitted a message from Andrew Thompson:
: On Wed, 24 Nov 2004 17:04:14 +0000 (UTC), Eduardo Bernal wrote:
: ..
: > String[] strArray;
: > strArray = new String[]
: // must end each statement with ';'
D'oh!
: strArray = new String[];
: > I've tried this with different IDEs (drjava, netBeans, and JCreator)
: You should try working the problem instead. You need to find out
: what causes the error, and how to fix it, because your IDE won't.
: > and they all complain
: No they don't. They merely report what the Java compiler
: is telling them.
Really? There's no little man in the magic picture box
upset because of an error I can't divine?
I'm a *java* newbie, it's not as if I've never programmed before.
And I dont think the IDEs compile the source every time I type a word,
or they're using something faster than javac. I'd figure that they'd be
parsing the source, without compiling it.
: >.aobut a missing identifier or
: > "identifier expected".
: First place to check errors..
: <http://mindprod.com/jgloss/compileerrormessages.html>
Good tip, but no help this time.
: <http://mindprod.com/jgloss/runerrormessages.html>
Good tip, but the snippet above never gets to be run
: > It looks like the example at the Sun java tutortial site.
: What example? URL?
http://java.sun.com/docs/books/tutorial/java/data/arraybasics.html
the first example, ArrayDemo
I get the error with this piece of code
public class ArrayBad{
int[] anArray;
anArray = new int[10];
}
: --
: Andrew Thompson
: http://www.PhySci.org/codes/ Web & IT Help
: http://www.PhySci.org/ Open-source software suite
: http://www.1point1C.org/ Science & Technology
: http://www.LensEscapes.com/ Images that escape the mundane
--
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
"Turns out that an American with sunglasses and listening to rock music,
naming his tank 'Anger Management' is a much, much deadlier guy than
a suicide bomber." - Victor Davis Hanson
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
|
|
| Back to top |
|
 |
Oscar kind Guest
|
Posted: Wed Nov 24, 2004 5:58 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
Eduardo Bernal <breetai (AT) otaku (DOT) freeshell.org> wrote:
| Quote: | why does this work:
String[] strArray = new String[10];
and this does not:
String[] strArray;
strArray = new String[]
|
Because you don't specify the size of the array. The expected identifier
is between the brackets (as the compiler probably told you) and specifies
the size of the array.
There are two ways to create an array:
strArray = new String[10]; // strArray holds 10 nulls
strArray = new String[]{"abc", "def"}; // strArray holds 2 strings
| Quote: | It looks like the example at the Sun java tutortial site.
|
It does. For you, it is also an example to always think about:
- what you are trying to do (create an array)
- what the compiler needs for that (its size)
- what the hints of the compiler mean (where you've missed something)
--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website
PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
|
|
| Back to top |
|
 |
karlheinz klingbeil Guest
|
Posted: Wed Nov 24, 2004 6:11 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
Eduardo Bernal schrub am Mittwoch, 24. November 2004
18:51 folgendes:
| Quote: | : No they don't. They merely report what the Java
: compiler is telling them.
Really? There's no little man in the magic picture
box upset because of an error I can't divine?
I'm a *java* newbie, it's not as if I've never
programmed before. And I dont think the IDEs compile
the source every time I type a word, or they're using
something faster than javac. I'd figure that they'd
be parsing the source, without compiling it.
|
Then you have to think again. Most IDE's (tell me which
does not) compile the code while you're typing it.
| Quote: | I get the error with this piece of code
public class ArrayBad{
int[] anArray;
anArray = new int[10];
}
|
Well, the missing Identifier is probably the
Constructor for this class... because the line
"anArray = new int(10)" is considered to be code, not
simply an initializer, this line must appear in a
method.
public class ArrayBad{
public ArrayBad(){
int[] anArray;
anArray = new int[10];
}
}
should work as expected.
--
greetz Karlheinz Klingbeil (lunqual)
http://www.lunqual.de oder http:www.lunqual.net
|
|
| Back to top |
|
 |
Yogo Guest
|
Posted: Wed Nov 24, 2004 6:17 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
"Eduardo Bernal" wrote:
| Quote: |
....
I get the error with this piece of code
public class ArrayBad{
int[] anArray;
anArray = new int[10];
}
|
That's because the statement "anArray = new int[10];" is not within a method
body. There should only be declarations (variables, classes, methods...) or
initializers outside method bodies.
Use an initializer to get your example compiled:
public class ArrayGood{
int[] anArray;
{ // begin instance initializer
// your statements here
anArray = new int[10];
} // end instance initializer
}
Yogo
|
|
| Back to top |
|
 |
Eduardo Bernal Guest
|
Posted: Wed Nov 24, 2004 8:08 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
On Wed, 24 Nov 2004 19:11:14 +0100, The Trilateral Commission's orbital mind control lasers
transmitted a message from karlheinz klingbeil:
: Eduardo Bernal schrub am Mittwoch, 24. November 2004
: 18:51 folgendes:
: Well, the missing Identifier is probably the
: Constructor for this class... because the line
: "anArray = new int(10)" is considered to be code, not
: simply an initializer, this line must appear in a
: method.
It seems to me that's it's odd that
int[] anArray = new int[10];
works outside of a method. Is this some quirk of Java?
Does it reveal something deep about the design of the language?
I thought that the one-liner above was functionally equivalent
to
int[] anArray;
anArray = new int[10];
The solution for me, then, is to use the one line version,
but I'd still like to know where this seeming inconsistency
comes from.
Thanks for you answer.
--
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
"Turns out that an American with sunglasses and listening to rock music,
naming his tank 'Anger Management' is a much, much deadlier guy than
a suicide bomber." - Victor Davis Hanson
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Wed Nov 24, 2004 9:21 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
Eduardo Bernal wrote:
| Quote: | On Wed, 24 Nov 2004 19:11:14 +0100, The Trilateral Commission's orbital mind control lasers
transmitted a message from karlheinz klingbeil:
: Eduardo Bernal schrub am Mittwoch, 24. November 2004
: 18:51 folgendes:
: Well, the missing Identifier is probably the
: Constructor for this class... because the line
: "anArray = new int(10)" is considered to be code, not
: simply an initializer, this line must appear in a
: method.
It seems to me that's it's odd that
int[] anArray = new int[10];
|
This is a declaration and initialization of the
identifier `anArray'.
| Quote: | works outside of a method. Is this some quirk of Java?
Does it reveal something deep about the design of the language?
I thought that the one-liner above was functionally equivalent
to
int[] anArray;
anArray = new int[10];
|
The first line is a declaration of `anArray', but
without an initializer. The second line is an assignment
statement, and that's the problem: executable statements
can only exist in methods and constructors -- and (here's
your way out, if you want it) in initializer blocks:
int[] anArray;
{
anArray = new int[10];
for (int i = 0; i < anArray.length; ++i)
anArray[i] = i * i - 42;
System.out.println("Hello, world!");
}
I'd suggest you avoid this form unless there's some
kind of computation that's *really* inconvenient to write
after the `=' of an initializer. "Keep It Simple, Stupid!"
as all my colleagues keep telling me ...
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
Oscar kind Guest
|
Posted: Wed Nov 24, 2004 9:30 pm Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
Eduardo Bernal <breetai (AT) otaku (DOT) freeshell.org> wrote:
| Quote: |
It seems to me that's it's odd that
int[] anArray = new int[10];
works outside of a method. Is this some quirk of Java?
Does it reveal something deep about the design of the language?
I thought that the one-liner above was functionally equivalent
to
int[] anArray;
anArray = new int[10];
|
It does seem odd, until you remember that you can initialize a member
variable while declaring it. I consider it not more than convenience
syntax though.
--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website
PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Sat Nov 27, 2004 9:01 am Post subject: Re: NEWBIE HELP: Missing identifier? |
|
|
On Wed, 24 Nov 2004 16:21:10 -0500, Eric Sosman <eric.sosman (AT) sun (DOT) com>
wrote:
| Quote: | The first line is a declaration of `anArray', but
without an initializer. The second line is an assignment
statement, and that's the problem: executable statements
can only exist in methods and constructors -- and (here's
your way out, if you want it) in initializer blocks:
int[] anArray;
{
anArray = new int[10];
for (int i = 0; i < anArray.length; ++i)
anArray[i] = i * i - 42;
System.out.println("Hello, world!");
}
I'd suggest you avoid this form unless there's some
kind of computation that's *really* inconvenient to write
after the `=' of an initializer. "Keep It Simple, Stupid!"
as all my colleagues keep telling me ...
|
And that can really really really not be put into an constructor.
The only use for non-static initializer blocks i have seen is for
anonymous inner classes, which (logically) have no explicit
constructors. And even there, i usually prefer an init method that
returns this.
--
Whom the gods wish to destroy they first call promising.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|