Help with method class value?
So, this is what I coded:
public char getInitials()
I thought this would work since the method type char is the same as the return type, but it says possible loss of precision, required char, found int on the lastName.charAt(0) line. Can someone please tell me what I may do to correct this?
Re: Help with method class value?
In your code you got that error because by default in Java the integer addition operator (+) will give you a result in an int data type. That is where the possible loss precision error comes from. It is because the value of int is much bigger than char.
But in you case as the initial will contain two characters you can't use the char data type as a return type there. Because a char only store a single character. If you want a string that have more than one character use the String object. If you are using the charAt() method you need to convert it first into string using the valueOf() method of the string class. Instead you can use String's substring() method to get the first letter from the firstname and the lastname.