Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java string problem

 
Martin Ctys
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I declared the int previously, and set it as "none". Inside the object, I also set MonsterName as none, or it would say MonsterObject.java:17: error: not a statement.
Okay, that got rid of the compiler error. I made a setter and getter method for the object to return the MonsterName:



No compiler errors so far.
A test drive:



Everything compiles correctly, but as the output I'm getting "none" after the name ( output: Name none )
I really need help, thank you for your time! I removed some irelevant code pieces, but if you want the full code, just tell me.
 
Praful Thakare
Ranch Hand
Posts: 642
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


do you see something wrong here?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Ctys wrote:

You have the assignment backwards. You need to make the variable being assigned to on the Left side, and the one being assigned from on the Right. This is a common mistake, and one I like to prevent by making the parameters to methods final:

If you do that then the compiler complains and you know exactly where the problem is!
 
Martin Ctys
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, you guys are great! So the parameter always goes after?
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the parameter always goes after?


don't remember it this way, remember that for assignment operator (=) the left side variable receives the value from right side variable (or constant or literal ).

-P
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that you know the order, since you wrote
in your code.

If you had tried instead, it would have been a compile error.


So, doing MonsterName = XName is exactly the same as doing MonsterName = "none". In both cases, you're taking the reference value in the RHS expression and copying it into the LHS variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic