• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Whats wrong in below code - toString returning Null

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output is
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0



expected output :

Name is Crish Salary: 2000
Name is Tom Salary: 2400
Name is Ram Salary: 3000
Name is John Salary: 6000


Please tell me where i going wrong
 
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
Lines 36 and 37 are the wrong way around. You're assigning the values of the member variables to the argument variables instead of the reverse.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in your constructor in lines 36 and 37.
If name refers to the argument of a constructor and this.name to the instance variable of the class, what do you think this assignments do?

Other issues.
1. Your Empl class should implement Comparable, not Comparator.
2. Do not compare ints in a way you do in line 60. This might result in an overflow and incorrect value. Use Integer.compare instead.
3. You should give your classes and variables meaningful names. So your class should be Employee, not Empl. Your sal variable should be salary etc.
4. Is employee with null name and 0 salary a valid one? If not, you do not need non-argument constructor.
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:The problem is in your constructor in lines 36 and 37.
If name refers to the argument of a constructor and this.name to the instance variable of the class, what do you think this assignments do?


Oh that was a blunder...

it worked fine.

Thanks
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't lived until you have made that mistake. Everybody does it. Just as everybody puts a semicolon after if and wonders why their if block doesn't seem to workAnd there are lots of other mistakes everybody makes. You mustn't worry about it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:1. Your Empl class should implement Comparable, not Comparator.


And it should be called Employee.

Don't make things cryptic just to save yourself a few keystrokes. Suppose you need to add an "Employer" class as well; what does that become: Emplr?

Winston
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Pawel Pawlowicz wrote:1. Your Empl class should implement Comparable, not Comparator.


And it should be called Employee.


I already said that :P
 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic