• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

ArrayList Question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was doing the code magnet exercisde on page 161 of HeadFirst Java and for some reason I cannot get it to compile.

I keep getting:

C:\javatests>javac ArrayListMagnet.java
ArrayListMagnet.java:7: '(' or '[' expected
ArrayList<String> a = new ArrayList<String>();
^
ArrayListMagnet.java:33: <identifier> expected
public static void printAL(ArrayList<String> al) {
^
ArrayListMagnet.java:40: ')' expected
^
3 errors


My code is the following:



I have looked this over twice, comparing it to the solution, and cannot find any differences. I thought it might be the version of SDK I am using, but I am running j2sdk1.4.2_10 on Windows XP and cannot find a later version.

Any advice that might help me sleep would be greatly appreciated.
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Welcome to JavaRanch!

That code needs to be compiled in Java 5. It uses generics which were introduced in Java 5 (and therefore not in Java 1.4).
 
Jeanne Boyarsky
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that even though Sun started calling Java 5, "5.0", they still use the "1.5" numbering scheme in the download URL.
 
Rob Clark
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank ya kindly, patna!
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pal,
I think u should use c:remove tag which belongs to the core
taglib of JSTL. Writing java code in JSP's is unacceptable.

look at http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kamesh Loganathan:
Hi Pal,
I think u should use c:remove tag which belongs to the core
taglib of JSTL. Writing java code in JSP's is unacceptable.

look at http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html



Huh? This question has nothing to do with JSP. The OP is writing a stand-alone appliction.

Also, stating that something is "unacceptable" seems a bit harsh as this is a matter of opinion. I think it is better to say that this practice is discouraged. But again, this isn't important in this thread since the question is NOT about JSPs.

Layne
[ January 22, 2006: Message edited by: Layne Lund ]
 
Rob Clark
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was the JDK I was using. It seems to work properly, now. Thank you very much for your assistance.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Clark:
It was the JDK I was using. It seems to work properly, now. Thank you very much for your assistance.



I'm glad to hear that you were able to fix the problem with the suggestions given here. Please come back with more questions.

Keep Coding!

Layne
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All!

First off, I would like to mention that I did do a forum search, and this thread is pretty close to the same problem I'm having. I'm working on the ArrayListMagnet exercise in HF Java and I think I have it typed correctly (I did add a closing brace that I didn't see in the book). I just checked my Java version, and I got back: Open JDK 1.6.0_22, which I'm thinking means that I'm using Java 6 (but I wouldn't bet my life on it). Below is my (heavily commented, sorry) code, if someone could take a look and help me figure out if I have a problem with the code syntax/logic, or if it is just due to a different version of Java.



The errors I get back from the compiler are:

ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
4 errors

Any help would be greatly appreciated. It actually took me an embarrassingly long time to make sense of that code, hence all the comments, and I'd like to get it to compile before moving on, if possible.

Regards,

Andrew

 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Kenyon wrote:Hello All!


The errors I get back from the compiler are:

ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state


It's a syntax error!
Look at the code closely. where you have put closing brace "}" for the main method.

By reading your code and comments it seems that you are trying to put a method definition in main().
 
Andrew Kenyon
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the quick reply. I should have mentioned in my original post that I had tried that, thinking that maybe the book really was right, lol. Still, with the "} // close main" removed, the program still won't compile. Below are the error messages:

ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:41: reached end of file while parsing
} // close class
^
5 errors


Illegal start of the expression? Is there something different between when the book was written and the version of Java I'm using? Maybe some weird difference because I'm using Open JDK? Or is it another syntax error on my part?
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Kenyon wrote:Thank you for the quick reply. I should have mentioned in my original post that I had tried that, thinking that maybe the book really was right, lol. Still, with the "} // close main" removed, the program still won't compile. Below are the error messages:

ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: illegal start of expression
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:32: ';' expected
public static void printAL(ArrayList<String> al) { //method to print AL state
^
ArrayListMagnet.java:41: reached end of file while parsing
} // close class
^
5 errors


Illegal start of the expression? Is there something different between when the book was written and the version of Java I'm using? Maybe some weird difference because I'm using Open JDK? Or is it another syntax error on my part?



There are many errors in your code:

1. cause of Illegal start of Expression error is that main() method should end at line 31.
2. even if you remove error 1 there is one more error: you can not call printAL() from main() because printAL() is non-static, and you can not call non-static members form static main() method. To correct this you should make printAL() static

After these correction your code should look like this:


 
Andrew Kenyon
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha! You, sir, are a genius. Or at least very helpful! Closing main() before printAL() did the trick and it compiled and ran. I thought there was a missing close bracket somewhere, which is why I just tried to put it at the end in the code I submitted.

Now I can go to sleep happy (and smarter). Thanks again.
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Joshi wrote:
2. even if you remove error 1 there is one more error: you can not call printAL() from main() because printAL() is non-static, and you can not call non-static members form static main() method. To correct this you should make printAL() static


Just ignore this one, you already had got it.

Andrew Kenyon wrote:
Aha! You, sir, are a genius. Or at least very helpful!


I am happy with helpful, because I don't think this problem was that challenging.
 
reply
    Bookmark Topic Watch Topic
  • New Topic