Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java substring issue

Posted on 2014-10-01
9
Medium Priority
?
169 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
[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
  • 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 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
DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

705 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