• Post Reply Bookmark Topic Watch Topic
  • New Topic

Identifier expected and illegal start of type errors  RSS feed

 
Emily Shephard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm working through Head First Java, and I'm struggling with the chapter 6 magnet exercise on page 161. I've checked my answer countless times, and it appears to match the book's code, but when I try to run it on my computer I'm getting an error. It says:
error: <identifier> expected
System.out.println^(" ");
and

error: illegal start of type
System.out.println(^" ");
, both for line 32. I cannot seem to figure out what is different about my code!!

Any help will be much appreciated!



Also, I'm not sure if this has anything to do with the above errors, but I'm getting a third error message for line 34 saying:

error: class, interface, or enum expected
}^
.

Thanks for your help! I feel like I'm overlooking something really obvious, but I just can't figure out what it is.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are caret characters (^) in your error messages that do not appear to be in the code you posted.

Suspicion: what editor are you using to write your code?
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emily,
Please try few modifications:
a) The line number 32 is outside the method printAL (there are two closing braces at line 30 and 31), I think you intended it to be just outside the for loop but inside of the method printAL. In that case remove one out of the two braces.
b) The type of the parameter in printAL method should be "ArrayList" and not "ArraryList".
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Illegal start of type compiler errors and class interface or enum expected (near the end of your code) mostly commonly arise from the same sort of cause: incorrectly paired braces. In my opinion it is easier to see brace mathcing if you use the indentation conventions shown here, but your use of K&R indentation appears to be correct and consistent, so if you simply gaze at the code you should be able to find an errant } which oughtn't to be there.

If you look at the Wikipedia link I quoted you see you appear to have Stroustrup's variant of K&R indentation. [That sounds like something about how rules are applied for Mornington Crescent. ]
 
Emily Shephard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much for your help!

Bear-
I am just using Notepad and Command Prompt as of now! I was just trying to represent the carets below the error message in Command Prompt. Sorry for the confusion. Thanks for your help!

Thanks again to all of you for your input! It compiled and ran without a problem. And I will definitely try the suggested formatting!
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it oiut

Don't use NotePad; it has some bad habits. There are other editors which you can use free of charge which are much better. We have an FAQ about it.
 
Emily Shephard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I will definitely look into that! Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!