Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Stranger compiler error?

Reply
Thread Tools

Stranger compiler error?

 
 
Knute Johnson
Guest
Posts: n/a
 
      11-10-2006
I found a strange compiler error when writing the code below. The next
program compiles and runs fine. In fact if I replace the error line
with the null statement it compiles just fine or if I just put the
braces around the error line it compiles. Needless to say, this has
been driving me nuts all morning!

Win XP Pro SP2
JDK 1.6.0-rc build 103

I haven't tried it on an older compiler.

Any ideas?

Thanks,

knute...


import java.util.*;

public class test1 {
public static void main(String[] args) throws Exception {
Hashtable<Integer,String[]> hash =
new Hashtable<Integer,String[]>();
hash.put(1,new String[] {"hello","world"});
hash.put(2,new String[] {"good","bye"});

for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements()
String[] array = hash.get(e.nextElement()); // <---- error
}
}

C:\com\knutejohnson\redrock\scores>javac test1.java
test1.java:10: '.class' expected
String[] array = hash.get(e.nextElement());
^
test1.java:10: not a statement
String[] array = hash.get(e.nextElement());
^
2 errors


import java.util.*;

public class test1 {
public static void main(String[] args) throws Exception {
Hashtable<Integer,String[]> hash =
new Hashtable<Integer,String[]>();
hash.put(1,new String[] {"hello","world"});
hash.put(2,new String[] {"good","bye"});

for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements() {
String[] array = hash.get(e.nextElement());
for (int i=0; i<array.length; i++)
System.out.println(array[i]);
}
}
}

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Richard F.L.R.Snashall
Guest
Posts: n/a
 
      11-10-2006
Knute Johnson wrote:
> In fact if I replace the error line
> with the null statement it compiles just fine or if I just put the
> braces around the error line it compiles.


And your aversion to braces is???
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      11-10-2006

Knute Johnson wrote:
> I found a strange compiler error when writing the code below. The next
> program compiles and runs fine. In fact if I replace the error line
> with the null statement it compiles just fine or if I just put the
> braces around the error line it compiles. Needless to say, this has
> been driving me nuts all morning!
>
> Win XP Pro SP2
> JDK 1.6.0-rc build 103
>
> I haven't tried it on an older compiler.
>
> Any ideas?
>
> Thanks,
>
> knute...
>
>
> import java.util.*;
>
> public class test1 {
> public static void main(String[] args) throws Exception {
> Hashtable<Integer,String[]> hash =
> new Hashtable<Integer,String[]>();
> hash.put(1,new String[] {"hello","world"});
> hash.put(2,new String[] {"good","bye"});
>
> for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements()
> String[] array = hash.get(e.nextElement()); // <---- error
> }
> }
>
> C:\com\knutejohnson\redrock\scores>javac test1.java
> test1.java:10: '.class' expected
> String[] array = hash.get(e.nextElement());
> ^
> test1.java:10: not a statement
> String[] array = hash.get(e.nextElement());
> ^
> 2 errors
>
>
> import java.util.*;
>
> public class test1 {
> public static void main(String[] args) throws Exception {
> Hashtable<Integer,String[]> hash =
> new Hashtable<Integer,String[]>();
> hash.put(1,new String[] {"hello","world"});
> hash.put(2,new String[] {"good","bye"});
>
> for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements() {
> String[] array = hash.get(e.nextElement());
> for (int i=0; i<array.length; i++)
> System.out.println(array[i]);
> }
> }
> }
>
> --
>
> Knute Johnson
> email s/nospam/knute/

You can't declare a variable in a one-statement block.
if (true) int something=10; // fails
if (true) { int something=10; } // succeeds.

Also, why not use HashMap?
Map<Integer, String[]> hash = new HashMap<Integer, String[]>();
// Do something with hash
for (Map.Entry<Integer, String[]> entry : hash.entrySet()) {
System.out.print(entry.getKey() + "->{");
for (String string : entry.getValue()) {
System.out.print(string + ", ");
}
System.out.println("}");
}

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      11-10-2006
"Richard F.L.R.Snashall" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Knute Johnson wrote:
>> In fact if I replace the error line with the null statement it compiles
>> just fine or if I just put the braces around the error line it compiles.

>
> And your aversion to braces is???


I think the problem isn't so much "How can I get this to compile?" but
rather "What's wrong with my understanding of the Java language?".

- Oliver


 
Reply With Quote
 
Richard F.L.R.Snashall
Guest
Posts: n/a
 
      11-10-2006
Oliver Wong wrote:
> "Richard F.L.R.Snashall" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Knute Johnson wrote:
>>> In fact if I replace the error line with the null statement it compiles
>>> just fine or if I just put the braces around the error line it compiles.

>> And your aversion to braces is???

>
> I think the problem isn't so much "How can I get this to compile?" but
> rather "What's wrong with my understanding of the Java language?".


So it's as simple as "a declarator block requires braces"?
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-10-2006
Knute Johnson wrote:
....
> for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements()
> String[] array = hash.get(e.nextElement()); // <---- error

....

You are confusing two very different entities, a statement and a
variable declaration.

The for MUST be followed by a statement.

You have a local variable declaration, which is not itself a statement
but can appear in a compound statement, such as a brace-enclosed block.

Incidentally, it seems a bit pointless because you assign to array and
then immediately leave its scope of declaration.

> C:\com\knutejohnson\redrock\scores>javac test1.java
> test1.java:10: '.class' expected
> String[] array = hash.get(e.nextElement());
> ^
> test1.java:10: not a statement
> String[] array = hash.get(e.nextElement());
> ^
> 2 errors


The first error message shows what happened when the compiler tried to
interpret the first few tokens as a statement, not a declaration.
"String[]" is not very likely as the start of a statement, but it could
have been something like "String[].class.notifyAll();". Not finding
".class" killed that idea.

The second message points out that it is not a statement, which is the
real problem.

Patricia
 
Reply With Quote
 
aldeabtalal@hotmail.com
Guest
Posts: n/a
 
      11-10-2006
Patricia Shanahan wrote:
> Knute Johnson wrote:
> ...
> > for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements()
> > String[] array = hash.get(e.nextElement()); // <---- error

> ...
>
> You are confusing two very different entities, a statement and a
> variable declaration.
>
> The for MUST be followed by a statement.
>
> You have a local variable declaration, which is not itself a statement
> but can appear in a compound statement, such as a brace-enclosed block.
>
> Incidentally, it seems a bit pointless because you assign to array and
> then immediately leave its scope of declaration.
>
> > C:\com\knutejohnson\redrock\scores>javac test1.java
> > test1.java:10: '.class' expected
> > String[] array = hash.get(e.nextElement());
> > ^
> > test1.java:10: not a statement
> > String[] array = hash.get(e.nextElement());
> > ^
> > 2 errors

>
> The first error message shows what happened when the compiler tried to
> interpret the first few tokens as a statement, not a declaration.
> "String[]" is not very likely as the start of a statement, but it could
> have been something like "String[].class.notifyAll();". Not finding
> ".class" killed that idea.
>
> The second message points out that it is not a statement, which is the
> real problem.
>
> Patricia


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      11-10-2006

"Richard F.L.R.Snashall" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Oliver Wong wrote:
>> "Richard F.L.R.Snashall" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Knute Johnson wrote:
>>>> In fact if I replace the error line with the null statement it compiles
>>>> just fine or if I just put the braces around the error line it
>>>> compiles.
>>> And your aversion to braces is???

>>
>> I think the problem isn't so much "How can I get this to compile?"
>> but rather "What's wrong with my understanding of the Java language?".

>
> So it's as simple as "a declarator block requires braces"?


Or that a variable declaration is not considered a statement (something
I didn't know either, until now). I don't think the OP had any doubts that
blocks require braces.

- Oliver


 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      11-11-2006
Patricia Shanahan wrote:
> Knute Johnson wrote:
> ...
>> for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements()
>> String[] array = hash.get(e.nextElement()); // <---- error

> ...
>
> You are confusing two very different entities, a statement and a
> variable declaration.
>
> The for MUST be followed by a statement.
>
> You have a local variable declaration, which is not itself a statement
> but can appear in a compound statement, such as a brace-enclosed block.


I understand the issue here I just can't find in the JLS where it is not
a statement. In fact, JLS 14.4.4 Execution of Local Variable
Declarations "A local variable declaration statement is an executable
statement.", would seem to say otherwise.

> Incidentally, it seems a bit pointless because you assign to array and
> then immediately leave its scope of declaration.


Yes.

>
>> C:\com\knutejohnson\redrock\scores>javac test1.java
>> test1.java:10: '.class' expected
>> String[] array = hash.get(e.nextElement());
>> ^
>> test1.java:10: not a statement
>> String[] array = hash.get(e.nextElement());
>> ^
>> 2 errors

>
> The first error message shows what happened when the compiler tried to
> interpret the first few tokens as a statement, not a declaration.
> "String[]" is not very likely as the start of a statement, but it could
> have been something like "String[].class.notifyAll();". Not finding
> ".class" killed that idea.
>
> The second message points out that it is not a statement, which is the
> real problem.
>
> Patricia


I think I found it - 14.4 Local Variable Declaration Statements
"Every local variable declaration statement is immediately contained by
a block. Local variable declaration statements may be intermixed freely
with other kinds of statements in the block."

This is a pretty subtle distinction but I can see the light now.

I can't think of a real situation where you would not have other code
mixed with the local variable declaration. Since you would then need an
enclosing block the error would never occur.

Thanks,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-11-2006
Knute Johnson wrote:
> Patricia Shanahan wrote:
>> Knute Johnson wrote:
>> ...
>>> for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements()
>>> String[] array = hash.get(e.nextElement()); // <---- error

>> ...
>>
>> You are confusing two very different entities, a statement and a
>> variable declaration.
>>
>> The for MUST be followed by a statement.
>>
>> You have a local variable declaration, which is not itself a statement
>> but can appear in a compound statement, such as a brace-enclosed block.

>
> I understand the issue here I just can't find in the JLS where it is not
> a statement. In fact, JLS 14.4.4 Execution of Local Variable
> Declarations "A local variable declaration statement is an executable
> statement.", would seem to say otherwise.


ASCII does not work well for some of this, because it lacks distinct
fonts. Also I should have capitalized "Statement". I meant the syntax
element "Statement", not the English word "statement".

Section 14.5 Statements lists all the productions for the syntax element
Statement. It does not include the syntax element
LocalVariableDeclarationStatement.

The production:

IfThenStatement:
if ( Expression ) Statement

definitely requires a syntax element Statement.

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New releases: Lost S3, Perfect Stranger & Veronica Mars: Updated complete downloadable R1 DVD DB & inof lists Doug MacLean DVD Video 6 06-15-2007 07:02 AM
Orson Welles The Stranger...any good transfers? DVD Video 1 01-20-2007 01:35 AM
DVD Verdict reviews: THE RINGER, WHEN A STRANGER CALLS (2006), and more! DVD Verdict DVD Video 0 05-16-2006 09:11 AM
HELP!!! What's the best version of Welles's The Stranger Edward Holub DVD Video 0 08-17-2005 02:18 AM
me again, even stranger problem. MajBach Computer Support 3 03-28-2005 04:45 PM



Advertisments