Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java substring issue

Posted on 2014-10-01
9
Medium Priority
?
174 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 17

Assisted Solution

by:krakatoa
krakatoa earned 668 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 668 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
 
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 36

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 28

Accepted Solution

by:
dpearson earned 664 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 28

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

926 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