• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the problem with my java code

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error is

 
Alexander Kober
Ranch Hand
Posts: 32
IntelliJ IDE Java Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see in line 10, the constructor of class 'Playing' takes a String argument. The field Playing#name is of type Playing.Name, an enum you defined. Java will not automatically convert a String to the corresponding enum value. In line 40, you actually do pass an enum to the constructor, so i guess all you'll want to do is replace line 10 with

 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the setNames method seems wrong. First of all, it doesn't set anything, so it should be called getName instead. Second, it can be implemented so much easier:
And should you ever need to convert a String into one of your enum constants, you can use Name.valueOf("thriller").
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander Kober wrote:As you can see in line 10, the constructor of class 'Playing' takes a String argument. The field Playing#name is of type Playing.Name, an enum you defined. Java will not automatically convert a String to the corresponding enum value. In line 40, you actually do pass an enum to the constructor, so i guess all you'll want to do is replace line 10 with


thanks for guiding me . my doubt is clear now but i want to ask 1 question which is
when i change to this so after changing this i thought it should not produce an error but it did , i m passing an String value which is accepted by constructor Playing(String name) then it should set the name to the value of constructor then i m calling the which should set the appropriate name according to the value of name variable into names variable
the error is
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can't use Strings inside switch statements. (Well, since Java 7 you can, but you'd need to compare them to String values.)
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Well, you can't use Strings inside switch statements. (Well, since Java 7 you can, but you'd need to compare them to String values.)

thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic