Solved

return string first half challenge

Posted on 2014-11-11
20
245 Views
Last Modified: 2014-11-26
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
20 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40435430
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435675
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40435714
Your solution works, although it is doing something it does not need to do.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 7

Author Comment

by:gudii9
ID: 40435724
although it is doing something it does not need to do.

i wonder what it is doing extra. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40436536
When would you want to skip line 5?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40437370
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40437443
Are you saying that you don't want to skip line 5?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40438149
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40438152
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40438880
You said
if i skip line 5 i cannot get first half right.
So don't skip line 5.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40441169
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441210
It is not necessary to cause line 5 to be skipped.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40444077
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40444188
the
if((i%2)==0)
causes line 5 to be skipped when i%2!=0
causing line 5 to be skipped is not necessary
0
 
LVL 7

Author Comment

by:gudii9
ID: 40456035
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40456331
It does not return anything when (i%2)!=0
0
 
LVL 7

Author Comment

by:gudii9
ID: 40457709
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?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40457722
Yes.
(and changing
   return null;
to
  return str+"kitten";
would also pass all tests)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40463230
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40463439
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)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question