• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code not compiling.

 
Saavan Hirani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I seem to be hacing a problem with this basic line of coding, I have checked my curly brackets and all seem to match up, but the <identifier> expected error is still appearing, help would be appreciated.



Firstly what I did was not have the 'class.' statement on the code however when compilin bluej asked me to use it, now this identifier error comes up. It is a simple accessor method and in the same program i have used 2 more with no problems.

Here is the full program code:





Thanks for all the help, very much appreciated from a newbie!
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saavan Hirani wrote:
Firstly what I did was not have the 'class.' statement on the code however when compilin bluej asked me to use it, now this identifier error comes up.


As you already guessed, it is the "class." that is causing the problem. Please remove it -- and then tell us what bluej complained about, that caused you to add it.

Henry
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use the dot to name anything in Java.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saavan, Welcome to JavaRanch
Few things are incorrect in your code. First you can't use dot(.) in method names. And your "pages" instance variable is declared as String but you are expecting an int in the "getPages()" method.
 
Saavan Hirani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, now it has been removed the compiler asks me to add '.class' again however to the line below that, the body of the accessor method in question.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just note your code. pages is a String. Your get method returns an int.
You need to change the type of the variable pages to int.
You create a variable in the constructor that is never used.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I don't think

return int pages;

will compile.

return pages;

might compile, if pages is the correct type for that method to return. Leandro has already told you, change the type of "pages" to int.
 
Saavan Hirani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh appologies, rookie mistake, I have now changed my field pages from String to an int however bluej would still like me to add the '.class'.

EDIT: Right, upon changing the field and removing the int type from the body of the accessor method the code compiled.

Thanks very much for the help, very much appreciated.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, you're on BlueJ? Not a program I like at all, I am afraid. I can't see why it wants you to add .class. Please post whatever code you have at present.
 
Saavan Hirani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right seeing that the problem is solved im relieved however im still not aware or what this '.class' is and its true function. Could someone enlighten me please?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly (and I probably don't) you put .class after the name of the class and it obtains the Class object for that class.

It doesn't load it; that Class object is already in memory. It simply finds a reference to it.

You will have to check however. It is an advanced feature, which there is a little bit about in the Class class documentation. Also something about it here, but not a lot.

That's the Class class documentation, not the class Class. I should go to the bottom of the class for saying that
 
Saavan Hirani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, nice one Campbell, thanks for all your help, much appreciated!! Will definatley return if I have anymore problems, which I most likely will.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome And you are bound to have more problems if I help you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic