Solved

java substring issue

Posted on 2014-10-01
9
166 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 36

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
A solution for Fortify Path Manipulation.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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…

632 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