I just want to return a string from this code. How can I fix this??
String foo(String s) {
return (12+Integer.parseInt(s.substring(0,2))).toString()+s.substring(2,8);
}
I just want to return a string from this code. How can I fix this??
String foo(String s) {
return (12+Integer.parseInt(s.substring(0,2))).toString()+s.substring(2,8);
}
Are you trying to return the integer sum as a string?
In java, using the +
operator on strings will append the strings rather than perform math addition.
If I understand your question correctly, you need to do something like this:
String foo(String s) {
return (12+Integer.parseInt(s.substring(0,2))+Integer.parseInt(s.substring(2,8)).toString());
}
This will add 12 + someInt + someOtherInt and then return the result as a String.
Change to
return String.valueOf(12+Integer.parseInt(s.substring(0,2)) + s.substring(2,8);