troubleshooting Question

return string first half challenge

Avatar of gudii9
gudii9Flag for United States of America asked on
Programming Languages-OtherJavaJava EE
20 Comments1 Solution435 ViewsLast Modified:

I am trying below challenge
i wrote as below
public String firstHalf(String str) {
int i=str.length();
return str.substring(0,(i/2)-1);

I got compilation error as below

Compile problems:

Error:      public String firstHalf(String str) {
This method must return a result of type String

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

see Example Code to help with compile proble

How to improve and fix my code
Please advise. thanks in advance
Join our community to see this answer!
Unlock 1 Answer and 20 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros