Solved

java substring issue

Posted on 2014-10-01
9
163 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 27

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 27

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 236
Weekend adv creator 3 64
Should localization be done inside spring controller 5 36
Print Rhino Java Array in Javascript 1 30
This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

735 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