Reputation: 32331
Hi all i need to typecast a String value into a char . Please help me , i am not sure how to do this This is my code
At First i have a Enum class as shown
public enum RemoteType {
@XmlEnumValue("Call")
Call("Call"),
@XmlEnumValue("Put")
Put("Put");
private final String value;
RemoteType(String v) {
value = v;
}
public String value() {
return value;
}
public static RemoteType fromValue(String v) {
for (RemoteType c: RemoteType.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
This Enum ( RemoteType) is present inside a DTO (FOrmBean ) as shown
public class Subform implements Serializable
{
private RemoteType remoteType;
public RemoteType getRemoteType()
{
return remoteType;
}
public void setRemoteType(RemoteType remoteType)
{
this.remoteType = remoteType;
}
}
I have another class called as MyObject as shown
class MyObject
{
public char side;
}
For this MyObject side property which is a char i want to assign this remoteType property of FormBean by calling Getter Method on it.
Now I am setting data inside the DTO Form Object as shown
Subform subform = new Subform();
subform.setRemoteType(RemoteType.Put);
After setting the data (IN JSP ) that , Now i am tying to extract it (IN Controller) and assign String to char as shown .
MyObject object = new MyObject();
object.side = Subform.getRemoteType().value(); // How can we assin (Here i am getting errror
saying cant assign string to char)
please help me
( The code is of jar files , so we cant change the Data types )
Upvotes: 0
Views: 330
Reputation: 9249
You can't cast an entire String
to a char
, but if your String
only has a single character in it, then you can simply do:
char theChar = myString.charAt(0);
Otherwise, you'll have to decide on how you can convert that String
into a char
- maybe you'll just grab the first/last character in the String
, or maybe you'll perform some algorithm that "sums up" the characters into a single character.
Either way, there's no general way for you to turn a String
into a char
as a String
is just a collection of char
s.
Upvotes: 3