• Post Reply Bookmark Topic Watch Topic
  • New Topic

<identifier> expected java error  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



when trying to compile i get the error <identifier> expected where shown
i have no idea whats causing this .. please help
 
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ella,


This method does not exist in the Player class. There is only the method

which accepts a String as arguement.
However, I do not see the need for the arguement in a getter method.
I suggest to have the following method in Player:


Cheers,
Michael
 
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes exactly. When you are using a getter method, you have to specify a return type in that method. that is how you will "get" any value right?

like micheal said, change the getter method to String getName(), return a string value from this. It could be like:

String getName()
{
String n = Keyboard.readString();
return n;
}

In your main method get the value by:
p1.name = p1.getName();
p2.name = p2.getName();

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the } in line 19.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . .
. . .


That is not how you implement a getXXX method. If there is a name field, you return that. That method gets a String from the keyboard, which is something totally different, and is probably best done by a utility class. you should really set the name in the constructor; that way you won’t have a nameless player.
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
. . .
. . .


That is not how you implement a getXXX method. If there is a name field, you return that. That method gets a String from the keyboard, which is something totally different, and is probably best done by a utility class. You should really set the name in the constructor; that way you won’t have a nameless player.


Yes i know that using the constuctor to set the name SHOULD be used. But according to the code i saw, i replied. And i usually have used getter methods with a return type, returning a value and hence replied. But got your point. Thanks
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ella Lautier wrote:when trying to compile i get the error <identifier> expected where shown
i have no idea whats causing this .. please help

the compiler errors are actually VERY helpful in finding problems. They give you a VERY good idea where the problem is. That is why we ask that when you ask about compiler errors, you post the full, complete text of the error. With it, people can easily look at your code to diagnose the problem. Without it, we have to either go through your code line by line and guess where the problem is, or cut-and-past it into files and try compiling it ourselves.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GetXXX() methods usually return a value. Full stop. They don’t take any parameters (except possibily in strange classes like this one, but nobody likes that class), don’t read from the keyboard or anything. Like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!