0

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);
}
Robert
  • 4,805
  • 16
  • 34
  • 46
Mayank Bist
  • 181
  • 1
  • 1
  • 6

2 Answers2

0

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.

0

Change to
return String.valueOf(12+Integer.parseInt(s.substring(0,2)) + s.substring(2,8);

Lankaka
  • 726
  • 4
  • 12