Solved

java substring issue

Posted on 2014-10-01
9
158 Views
Last Modified: 2014-10-05
Hi,

I am trying below challenge
http://codingbat.com/prob/p183592

I wrote as below

public String front22(String str) {
if(str.length()>=2){
return str.substring(0,2)+str+str.substring(0,2);
}
if(str.length()==2){
return str+str+str;
}
if(str.length()==1){
return str+str+str;
}
  return null;
}


My test case failing for below scenario
front22("") → ""	"null"	X


Please advise

Open in new window

0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40355725
i tried below
public String front22(String str) {
if(str.length()>=2){
return str.substring(0,2)+str+str.substring(0,2);
}
if(str.length()==2){
return str+str+str;
}
if(str.length()==1){
return str+str+str;
}

if(str.length()==""){
return "";
}
  return null;
}

but not working. I wonder why? please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 40355735
Please clarify what isn't working about the code you posted.
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 167 total points
ID: 40355820
if(str.length()==""){

You can't do this for one thing - length() returns an int not a void or null string.

And you do realise that this section of code :

if(str.length()==2){
 return str+str+str;
 }

Open in new window


will be completely ineffective, because any string of length 2 will be captured by the if clause above it, which states ">=2".
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 167 total points
ID: 40356051
if(str.length()==""){
return "";
}
In addition to what Krak said about the validity of the above code, you don't need to test for anything here anyway. If you got to this point then str.length() must be 0 (otherwise it would have been handled by the other "if" statements), so just do the....    return "";       by itself, no if statement at all.  (And then you will need to get rid of the    return null;    below)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Author Comment

by:gudii9
ID: 40356167
public String front22(String str) {
if(str.length()>=2){
return str.substring(0,2)+str+str.substring(0,2);
}
if(str.length()==2){
return str+str+str;
}
if(str.length()==1){
return str+str+str;
}


return "";

}

Open in new window


I see above one is working for all scenarios.

Having multiple retuns in each if and also outside if loops at the end of the method looks bit odd to me. Is that is general practice please advise
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40356176
You can't say that it is necessarily the "general practice" but it is certainly one possible valid practice, it just depends on the situation. The one definite thing here is that since your function specifies that it will return a String, you need to make sure that every possible code execution path returns a value that can be type String. In this case, if any of the "if" statements match then it will return the String that is specified within them. If none of the "if" match then code execution will continue further and reach the    return "";   which ensures that a String value is returned. Nothing terribly odd about it.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 166 total points
ID: 40356336
The code you have will work, but as I suggested in an earlier question having a lot of 'ifs' is not ideal.

You might want to consider a solution like this:

public String front22(String str) {
  String front = str.substring(0, Math.min(2,str.length())) ;
  return front + str + front ;
}

Can you see why this works?

It's doing the same thing as your different if statements but rolled up into one command.

To figure it out, I'd suggest working through what will happen when you pass this with a string of length 3.  Then one of length 2.  Then one of length 1.

If you can understand how this is working, you may find you can use it in similar situations.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40358557
Math.min(2,str.length()

what is the  purpose of above line. Please advise
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40358680
Math.min(a,b) returns the smaller of the two values "a" and "b".

So

Math.min(2, str.length()) returns either the length of the string or the value 2, whichever is smaller.

So for a string longer than 2 it returns 2.
For a string length 1, it returns 1.
For a string length 0, it returns 0.

Can you see now how that makes the whole thing work?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now