This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Identifier expected

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I am a total beginner, so don't laugh Basically, I have the following problem:



While compiling, one identifier is expected. I don't know, why. I have identified Kg as well as g...

weight.java:4: error: <identifier> expected
       g = 0.001*Kg;
        ^
1 error



Thanks for your help!
 
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please call your class Weight rather than weight. I am not convinced it is a good idea to have two co‑dependent fields; if you change g without changing Kg (under the usual conventions that should read kg), you'll have an error in the state of your object.

As for the compiler error, the bit with 0001* in is a statement; statements must be in a method, constructor, etc., rather than just lying loose in the body of the class. If you say the error message isn't very helpful, I think I shall agree with you.
 
Marshal
Posts: 24853
60
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 4 is an assignment statement, so it must be inside a method or a constructor or an initializer.
 
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For experimentation, I would suggest creating a main() method.

This code might be a good starting point (put inside your class):



----

I would also pick a much better variable name than "g". A program (and variable names) that seems obvious while you're working on it can look totally foreign in a few months or even weeks. So, creating self-documenting variable names is a good practice for you or for the person who might have to maintain your code later.

HTH

-- mike
 
lowercase baba
Posts: 12785
51
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike London wrote:It can look totally foreign in a few months or even weeks.


Shoot, when you reach my age, it's a few hours...
 
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better yet, realize that what you have there is a conversion formula and that sort of thing might be better represented as a function/method, like so:


This also shows that the names you have used, g and Kg, suggest you have used the wrong formula. When converting kilos to grams, you should expect a larger number, not a smaller one.
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Better yet, realize that what you have there is a conversion formula and that sort of thing might be better represented as a function/method, like so:


This also shows that the names you have used, g and Kg, suggest you have used the wrong formula. When converting kilos to grams, you should expect a larger number, not a smaller one.



That's a good idea if you, Mr. OP, plan to call the code more than once: always make a method. And, pick a good method name as was shown above.

However, since your question was very basic, I didn't elaborate or assume any requirements (I just answered your question).

Enjoy!

-- mike
 
What's brown and sticky? ... a stick. Or a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!