Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

<identifier> expected java error

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 54
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the } in line 19.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Hold that thought. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic