File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Question" Watch "ArrayList Question" New topic
Author

ArrayList Question

Rob Clark
Greenhorn

Joined: Jan 22, 2006
Posts: 7
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
134

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).


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
134

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

Joined: Jan 22, 2006
Posts: 7
Thank ya kindly, patna!
Kamesh Loganathan
Greenhorn

Joined: Jan 21, 2006
Posts: 20
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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Rob Clark
Greenhorn

Joined: Jan 22, 2006
Posts: 7
It was the JDK I was using. It seems to work properly, now. Thank you very much for your assistance.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
Andrew Kenyon
Greenhorn

Joined: Dec 23, 2010
Posts: 5
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

Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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().

Piyush
Andrew Kenyon
Greenhorn

Joined: Dec 23, 2010
Posts: 5
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

Joined: Jun 10, 2011
Posts: 207

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

Joined: Dec 23, 2010
Posts: 5
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

Joined: Jun 10, 2011
Posts: 207

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Question
 
Similar Threads
error with latest jdk
Having some trouble with this code.
Can the index number be specified in the ArrayList add method
The Programme is not Compiling
ArrayListMagnet- problem compiling