Avatar of gudii9
gudii9
Flag for United States of America asked on

return string first half challenge

Hi,

I am trying below challenge
http://codingbat.com/prob/p172267
i wrote as below
public String firstHalf(String str) {
int i=str.length();
if((i/2)==0)
return str.substring(0,(i/2)-1);
  
}

Open in new window


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
JavaJava EEProgramming Languages-Other

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
ozo

Follow the advice given in the message, and add a return statement that happens even when the if condition is not satisfied.

Or consider whether you even need the if condition.
gudii9

ASKER
public String firstHalf(String str) {
String str2=null;
int i=str.length();
if((i%2)==0)
str2= str.substring(0,(i/2));
return str2;
  
}

Open in new window


I modified as above and pased all tests

Expected      Run            
firstHalf("WooHoo") → "Woo"      "Woo"      OK         
firstHalf("HelloThere") → "Hello"      "Hello"      OK         
firstHalf("abcdef") → "abc"      "abc"      OK         
firstHalf("ab") → "a"      "a"      OK         
firstHalf("") → ""      ""      OK         
firstHalf("0123456789") → "01234"      "01234"      OK         
firstHalf("kitten") → "kit"      "kit"      OK         
other tests
OK         


Does my solution look fine?
please advise
ozo

Your solution works, although it is doing something it does not need to do.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
gudii9

ASKER
although it is doing something it does not need to do.

i wonder what it is doing extra. please advise
ozo

When would you want to skip line 5?
gudii9

ASKER
When would you want to skip line 5?
if i skip line 5 i cannot get first half right. Please advise what i am missing
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ozo

Are you saying that you don't want to skip line 5?
gudii9

ASKER
public String firstHalf(String str) {
String str2=null;
int i=str.length();
if((i%2)==0)
//str2= str.substring(0,(i/2));
return str2;
  
}

Open in new window


when i skip line 5 i am getting compilation error



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 problems
gudii9

ASKER
Your solution works, although it is doing something it does not need to do.

I would like to know what my code doing something it does not need to do so that i correct it. Please advise
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ozo

You said
if i skip line 5 i cannot get first half right.
So don't skip line 5.
gudii9

ASKER
Your solution works, although it is doing something it does not need to do.


I would like to know what my code doing something it does not need to do so that i correct it. Please advise

please advise on this.
ozo

It is not necessary to cause line 5 to be skipped.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gudii9

ASKER
public String firstHalf(String str) {
String str2=null;
int i=str.length();
if((i%2)==0)
str2= str.substring(0,(i/2));
return str2;
 
}

so what is the improvement i can make to my code.

I did not get your below point
although it is doing something it does not need to do.

please advise
ozo

the
if((i%2)==0)
causes line 5 to be skipped when i%2!=0
causing line 5 to be skipped is not necessary
gudii9

ASKER
public String firstHalf(String str) {
String str2=null;
int i=str.length();
if((i%2)==0)
//str2= str.substring(0,(i/2));
return str.substring(0,i/2);
  
}

Open in new window


are you saying directly returning as above is preferable.

I am getting 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 problems


please advise
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ozo

It does not return anything when (i%2)!=0
gudii9

ASKER
the
if((i%2)==0)
causes line 5 to be skipped when i%2!=0
causing line 5 to be skipped is not necessary

i still did not understand what you mean.

My code now works and passes all tests as below
public String firstHalf(String str) {
String str2=null;
int i=str.length();
if((i%2)==0)
//str2= str.substring(0,(i/2));
return str.substring(0,i/2);
else
return null;
  
}

Open in new window


So i took care of your suggestion in my above code right?
ASKER CERTIFIED SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
gudii9

ASKER
public String firstHalf(String str) {
String str2=null;
int i=str.length();
if((i%2)==0)
//str2= str.substring(0,(i/2));
return str.substring(0,i/2);
else
return str+"kitten";
 
}


i changed as above and passed all tests. But how it passed all tests. For odd number string (say "xyz") it goes into else loop right. then str+"kitten" should return "xyzkitten"

Is xyzkitten correct value?
please advise
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ozo

The challenge says
Given a string of even length
so (i%2)==0 is never false

Is xyzkitten correct value?
We don't know.  The challenge says nothing about what firstHalf("xyz") should yield.
However, there may be arguments for expecting "xy" or "x" rather than "null"
(which is what firstHalf("nullings") should yield)