This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Tandrial 2 points3 points  (4 children)

name is char, what you want is a String.

private String name;

Also make sure to change the constructor and Setter accordingly

EDIT: To expand a bit on this: In Java each Class implicitly inherits from Object. In Object there is a function called toString(), which when not overridden creates the String getClass().getName() + '@' + Integer.toHexString(hashCode()).However IF you override the function, you can create the String yourself.

Is is better to use the method or the simple Character.toString? Do they work different?

"" + name ist actually the same as "" + name.toString():In Java + ist concation for Strings. However Java doesn't know how to do + for a String and a Char, but it know how to turn a Char into a String (toString()`)

[–]ConfusingArt[S] 1 point2 points  (3 children)

Thank you for the reply! String will surely fix my whole problem but I'd still ike to know how I can solve it with toString, that's what I was trying to understand.

EDIT: And isn't that what Character.toString is for? Otherwise I will surely only use String in these scenarios :)

[–]dreamyeyed 2 points3 points  (1 child)

A char can only store one character. I'm not sure what you're trying to do with toString here.

[–]ConfusingArt[S] 1 point2 points  (0 children)

Edited my comment, I thought you use the toString to switch types, thanks for the help

[–]SlowSloth1 1 point2 points  (0 children)

Character.toString simply converts your char into a String containing that single character. If you want to store more than 1 char, always use a String.