[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

without x challenge

Posted on 2015-01-16
58
Medium Priority
?
116 Views
Last Modified: 2015-02-12
Hi,

I am trying below challenge

http://codingbat.com/prob/p151940

i tried as below

public String withoutX(String str) {
int strLen=str.length();
String st=null;
if(str.substring(0,1).equals("x") | str.substring(strLen-1).equals("x"))
{
st=str.substring(strLen-2);
}
return str;
  
}

Open in new window

I am failing test cases. Please advise on how to fix and improve.
Expected	Run		
withoutX("xHix") → "Hi"	"xHix"	X	    
withoutX("xHi") → "Hi"	"xHi"	X	    
withoutX("Hxix") → "Hxi"	"Hxix"	X	    
withoutX("Hi") → "Hi"	"Hi"	OK	    
withoutX("xxHi") → "xHi"	"xxHi"	X	    
withoutX("Hix") → "Hi"	"Hix"	X	    
withoutX("xaxbx") → "axb"	"xaxbx"	X	    
withoutX("xx") → ""	"xx"	X	    
withoutX("x") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
withoutX("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (line number:4)"	X	    
withoutX("Hello") → "Hello"	"Hello"	OK	    
withoutX("Hexllo") → "Hexllo"	"Hexllo"	OK	    
other tests
X	

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
  • 27
  • 15
  • 10
  • +1
58 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 668 total points
ID: 40554416
Your computation of st is useless when st is never used.

str.substring(strLen-2) takes the last two characters of str, you want str without the first and/or last x
0
 
LVL 7

Author Comment

by:gudii9
ID: 40554430
public String withoutX(String str) {
int strLen=str.length();
String st=null;
if(str.substring(0,1).equals("x") | str.substring(strLen-1).equals("x"))
{
st=str.substring(strLen-2);
}
return st;
  
}

Open in new window


i fixed by removing from str.

i passed one test but failing others

Expected	Run		
withoutX("xHix") → "Hi"	"ix"	X	    
withoutX("xHi") → "Hi"	"Hi"	OK	    
withoutX("Hxix") → "Hxi"	"ix"	X	    
withoutX("Hi") → "Hi"	"null"	X	    
withoutX("xxHi") → "xHi"	"Hi"	X	    
withoutX("Hix") → "Hi"	"ix"	X	    
withoutX("xaxbx") → "axb"	"bx"	X	    
withoutX("xx") → ""	"xx"	X	    
withoutX("x") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
withoutX("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (line number:4)"	X	    
withoutX("Hello") → "Hello"	"null"	X	    
withoutX("Hexllo") → "Hexllo"	"null"	X	    
other tests

Open in new window


str.substring(strLen-2) takes the last two characters of str, you want str without the first and/or last x
not getting idea on how to do this. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40554522
Do you remember how to remove the first character of a string?  Do you remember how to remove the last character of a string?
0
Independent Software Vendors: 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 32

Assisted Solution

by:awking00
awking00 earned 668 total points
ID: 40555301
String a = str.startsWith("x") ? str.substring(1) : str; //removes first x if it exists
String b = a.endsWith("x") ? a.substring(0,a.length() - 1): a; //removes last x from string a if it exists
return b;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40558634
public String withoutX(String str) {
int strLen=str.length();
String st=null;
if(String a = str.startsWith("x") ? str.substring(1) : str;))
{
return a;
}
if(String b = a.endsWith("x") ? a.substring(0,a.length() - 1): a;))
{
return b;
}
return null;
}

        

Open in new window



something like above .

I am getting error

ompile problems:


Error:      if(String a = str.startsWith("x") ? str.substring(1) : str;))
         ^^^^^^
Syntax error, insert ") Statement" to complete IfStatement


see Example Code to help with compile problems

Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40558697
You can't put a declaration in an if clause like that.
And you don't need to, since
String a = str.startsWith("x") ? str.substring(1) : str;
is a complete statement in itself.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 664 total points
ID: 40559275
gudii9,
What you need to learn - more then writing the resulting java code - is thinking on HOW TO HANDLE A PROBLEM.
If you (instead of the program) would do it your self, how would you do it?

This way of working passes all tests:
1) take a String variable named 'result' and copy the value of the incoming parameter 'str' into it.
2) if the value of the String 'result' starts with a "x", then chop off the first character of 'result'
3) if the value of the String 'result' ends with a "x", then chop off the last character of 'result'
4) return the String 'result'
0
 
LVL 7

Author Comment

by:gudii9
ID: 40559831
public String withoutX(String str) {
int strLen=str.length();
String result=str;
result = str.startsWith("x") ? str.substring(1) : str;
result = str.endsWith("x") ? str.substring(0,str.length() - 1): str;
//result=(str.startsWith("x")&&str.endsWith("x") )? //(str.substring(1)+str.substring(0,str.length() - 1)) : str;
return result;

}

Open in new window


i got the approach i should take. I tried as above. I fail in few test cases

Expected	Run		
withoutX("xHix") → "Hi"	"xHi"	X	    
withoutX("xHi") → "Hi"	"xHi"	X	    
withoutX("Hxix") → "Hxi"	"Hxi"	OK	    
withoutX("Hi") → "Hi"	"Hi"	OK	    
withoutX("xxHi") → "xHi"	"xxHi"	X	    
withoutX("Hix") → "Hi"	"Hi"	OK	    
withoutX("xaxbx") → "axb"	"xaxb"	X	    
withoutX("xx") → ""	"x"	X	    
withoutX("x") → ""	""	OK	    
withoutX("") → ""	""	OK	    
withoutX("Hello") → "Hello"	"Hello"	OK	    
withoutX("Hexllo") → "Hexllo"	"Hexllo"	OK	    
other tests
X	    

Open in new window


please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40559861
2) if the value of the String 'result' starts with a "x", then chop off the first character of 'result'
i am rereading i think i have to change my code as per above suggestion.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40559864
public String withoutX(String str) {
int strLen=str.length();
String result=str;
result = result .startsWith("x") ? result .substring(1) : str;
result = result .endsWith("x") ? result .substring(0,str.length() - 1): str;
//result=(str.startsWith("x")&&str.endsWith("x") )? //(str.substring(1)+str.substring(0,str.length() - 1)) : str;
return result;

}

Open in new window


like above?

still failing some cases. please advise
0
 
LVL 32

Expert Comment

by:awking00
ID: 40559924
>>like above?<<
No!
No need to assign the str parameter to a new variable so the first statement should be something like -
String removefrontx = str.startsWith("x") ? str.substring(1) : str;
Stated in layman's terms means in the case when the str in starts with x set the value of the substring of x beginning with the second character to the variable, removefrontx, otherwise set the value to the string.
Now you have a removefrontx string that has no x in front whether or not there was in the original string.
So now you can do the same thing with the removefrontx string to remove the last character x if it exists -
String result = removefromtx.endsWith("x") ? removefrontx.substring(0,removefrontx.length() - 1) : removefrontx;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40559986
>> 1) take a String variable named 'result' and copy the value of the incoming parameter 'str' into it.
String result = str;

Open in new window


>> 2) if the value of the String 'result' starts with a "x", then chop off the first character of 'result'
if (result.startsWith("x")) {
     result = result.substring(1);
}

Open in new window


3) if the value of the String 'result' ends with a "x", then chop off the last character of 'result'
if (result.endsWith("x")) {
  result = result.subString(0, result.length() - 1);
}

Open in new window


4) return the String 'result'
return result;

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40560016
i see the point.

public String withoutX(String str) {
String result = str;
if (result.startsWith("x")) {
     result = result.substring(1);
}



if (result.endsWith("x")) {
  result = result.subString(0, result.length() - 1);
}

return result;

}

Open in new window

i am getting compilation error. please advise
Compile problems:


Error:      result = result.subString(0, result.length() - 1);
                      ^^^^^^^^^
The method subString(int, int) is undefined for the type String


see Example Code to help with compile problems
0
 
LVL 32

Expert Comment

by:awking00
ID: 40560033
The substring method is all lower case (i.e. not subString)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40560095
awking00 is right.
gudii9, no problem that you ask this, but I would expect you to find that yourself. (after having done that bunch of String examples.)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40562183
sure. got it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40562185
public String withoutX(String str) {
String result = str;
if (result.startsWith("x")) {
     result = result.substring(1);
}



if (result.endsWith("x")) {
  result = result.substring(0, result.length() - 1);
}

return result;

}

Open in new window


above passed all.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40562186
You can't put a declaration in an if clause like that.
And you don't need to, since
String a = str.startsWith("x") ? str.substring(1) : str;
is a complete statement in itself

How to do this challenge using regular expression
...?...:...

Please advise
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40562195
That's no regular expression, that's the so-called conditional operator

condition ? this() : that();

Open in new window


is just another way of writing of this:

if (condition) {
  this();
} else {
   that();
}

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 40562201
So if you  would like you could write the code as:
public String withoutX(String str) {
   String result = str.startsWith("x") ? str.substring(1) : str;
   return result.endsWith("x") ? result.substring(0, result.length() - 1) : result;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40562242
That's no regular expression, that's the so-called conditional operator
oh something like short cut conditional operator. What is regular expression concept used in java. I remember reading some where in recent posts about it. How do we use reular expression in these kind of challenges. please advise
0
 
LVL 32

Expert Comment

by:awking00
ID: 40562892
You could use regular expressions something like the following;
return str.replaceFirst("^x", "").replaceFirst("x$", "");
Where the regular expression ^x represents an x at the beginning and the expression x$ represents an x at the end.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40563737
Here's a good site dedicated to regular expressions; http://www.regular-expressions.info/
0
 
LVL 7

Author Comment

by:gudii9
ID: 40570748
public String withoutX(String str) {
   String result = str.startsWith("x") ? str.substring(1) : str;
   return result.endsWith("x") ? result.substring(0, result.length() - 1) : result;
}

Open in new window

passed all the tests.


Given a string, if the first and last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.
i wonder if the challenge would have given and not or then how above solution needs to be changed. In that case i cannot use this conditional operator right?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40570752



public String withoutX(String str) {
   return str.replaceFirst("^x", "").replaceFirst("x$", "");
}

all tests passed with above code.

return str.replaceFirst("^x", "").replaceFirst("x$", "");
Where the regular expression ^x represents an x at the beginning and the expression x$ represents an x at the end.

Open in new window


How ^ represent x at the start and  $ represent x at the end. That does not make sense to me. please elaborate.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40570792
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40570803
>> i wonder if the challenge would have given and not or then how above solution needs to be changed.

if ( str.startsWith("x") && str.endsWith("x") ) {
    return str.substring(1, str.length() - 2);
}

Which you can again also write using a conditional operator
0
 
LVL 32

Expert Comment

by:awking00
ID: 40571048
>>How ^ represent x at the start and  $ represent x at the end. That does not make sense to me. please elaborate.<<
Because "^" and "$" are metacharacters in the regular expression vernacular with "^" indicating the start of a line (string) and "$" indicating the end of a line. There are many more metacharacters in regular expressions each with different meanings. There are also many sources, that discuss regular expressions, available on the web. Don't expect to become a regular expression expert in a short period of time. I've been using them for years and still only consider myself somewhat of a novice.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40571065
>>I came across two links. I still need to digest the content after re reading few times<<
For someone still struggling somewhat with simple string manipulations, that could be an effort in futility. I have nearly 100 Java utilities with thousands of lines of code that I use in my work and only one uses a regular expression.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40571326
>> i wonder if the challenge would have given and not or then how above solution needs to be changed.

if ( str.startsWith("x") && str.endsWith("x") ) {
    return str.substring(1, str.length() - 2);
}

Which you can again also write using a conditional operator

public String withoutX(String str) {
   //return str.replaceFirst("^x", "").replaceFirst("x$", "");
   String result=str;
   
   int strLen=str.length();
   
   if(strLen>2){
   if ( str.startsWith("x") && str.endsWith("x") ) 
    result=str.substring(1, str.length() - 2);
}
return result;
}

Open in new window


i wrote as above  failing tests.
Expected	Run		
withoutX("xHix") → "Hi"	"H"	X	    
withoutX("xHi") → "Hi"	"xHi"	X	    
withoutX("Hxix") → "Hxi"	"Hxix"	X	    
withoutX("Hi") → "Hi"	"Hi"	OK	    
withoutX("xxHi") → "xHi"	"xxHi"	X	    
withoutX("Hix") → "Hi"	"Hix"	X	    
withoutX("xaxbx") → "axb"	"ax"	X	    
withoutX("xx") → ""	"xx"	X	    
withoutX("x") → ""	"x"	X	    
withoutX("") → ""	""	OK	    
withoutX("Hello") → "Hello"	"Hello"	OK	    
withoutX("Hexllo") → "Hexllo"	"Hexllo"	OK	    
other tests
X	    

Open in new window


 please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40571703
Consider one of the failing tests.
Can you understand what your code does in that case?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40572201
You had already code that passed all tests
0
 
LVL 7

Author Comment

by:gudii9
ID: 40572934
I see i am trying AND chalenge instead of OR for my own curiosity. Part of the reason is i am testing on the codingbat page which tests only OR case by default but not AND case.

I sued eclipse and wrote as below and passed all the tests for AND also. Any other improvements for my code please advise.

public class Test47 {

	/**
	 * @param args
	 */
	
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	   System.out.println("withoutX xHix is---"+withoutX("xHix"));
	   System.out.println("withoutX xHi is---"+withoutX("xHi"));
	   System.out.println("withoutX Hxix is---"+withoutX("Hxix"));
	   System.out.println("withoutX Hi is---"+withoutX("Hi"));
	   System.out.println("withoutX xxHi is---"+withoutX("xxHi"));
	   System.out.println("withoutX Hix is---"+withoutX("Hix"));
	   System.out.println("withoutX xaxbx is---"+withoutX("xaxbx")) ;	    
	   System.out.println("withoutX xx is---"+withoutX("xx"));	    
	   System.out.println("withoutX x is---"+withoutX("x")) ;
	   System.out.println("withoutX empty is---"+withoutX(""));	    
	   System.out.println("withoutX Hello is---"+withoutX("Hello")) ;
	   System.out.println("withoutX Hexllo is---"+withoutX("Hexllo")) ;   
	  
	}
	
	
	
	
	public static String withoutX(String str) {
		   //return str.replaceFirst("^x", "").replaceFirst("x$", "");
		   String result=str;
		   
		   int strLen=str.length();
		   
		   if(strLen>=2){
		   if ( str.startsWith("x") && str.endsWith("x") ) 
		    result=str.substring(1, str.length() - 1);
		}
		return result;
		}

}

Open in new window



console is

withoutX xHix is---Hi
withoutX xHi is---xHi
withoutX Hxix is---Hxix
withoutX Hi is---Hi
withoutX xxHi is---xxHi
withoutX Hix is---Hix
withoutX xaxbx is---axb
withoutX xx is---
withoutX x is---x
withoutX empty is---
withoutX Hello is---Hello
withoutX Hexllo is---Hexllo
0
 
LVL 84

Expert Comment

by:ozo
ID: 40572959
You now have it working for "xHix"
Can you also make it work for "xHi" or for "Hix"?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40573022
>> Any other improvements for my code please advise.
Again, what is wrong with this working code:
public String withoutX(String str) {
   String result = str.startsWith("x") ? str.substring(1) : str;
   return result.endsWith("x") ? result.substring(0, result.length() - 1) : result;
}

Open in new window


If you don't like those ? operators, rewrite it with ifs and you're done.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40580689
nothing wrong. I am just exploring multiple ways of doing and learning the mistakes in those trials.

public String withoutX(String str) {		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(0,1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+lastChar+rest;


}

Open in new window


i wrote as above failing some test cases
Expected	Run		
withoutX("xHix") → "Hi"	"Hi"	OK	    
withoutX("xHi") → "Hi"	"xH"	X	    
withoutX("Hxix") → "Hxi"	"Hxi"	OK	    
withoutX("Hi") → "Hi"	"HH"	X	    
withoutX("xxHi") → "xHi"	"xxH"	X	    
withoutX("Hix") → "Hi"	"Hi"	OK	    
withoutX("xaxbx") → "axb"	"axb"	OK	    
withoutX("xx") → ""	""	OK	    
withoutX("x") → ""	""	OK	    
withoutX("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (line number:6)"	X	    
withoutX("Hello") → "Hello"	"HHell"	X	    
withoutX("Hexllo") → "Hexllo"	"HHexll"	X	    
other tests
X

Open in new window


please advise
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40585662
>> String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(0,1);
Reread and rethink the above line and find for yourself what is wrong.
If you tried that, but couldn't find any error, then come back and ask here.

Do the same for this line:
>> return firstChar+lastChar+rest;

And also think about handling the special/corner cases first.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592250
public String withoutX(String str) {		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+rest+lastChar;


}

Open in new window


i see mistakes i did and fixed them now as above, failing one test case. please advise

Expected	Run		
withoutX("xHix") → "Hi"	"Hi"	OK	    
withoutX("xHi") → "Hi"	"Hi"	OK	    
withoutX("Hxix") → "Hxi"	"Hxi"	OK	    
withoutX("Hi") → "Hi"	"Hi"	OK	    
withoutX("xxHi") → "xHi"	"xHi"	OK	    
withoutX("Hix") → "Hi"	"Hi"	OK	    
withoutX("xaxbx") → "axb"	"axb"	OK	    
withoutX("xx") → ""	""	OK	    
withoutX("x") → ""	""	OK	    
withoutX("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (line number:6)"	X	    
withoutX("Hello") → "Hello"	"Hello"	OK	    
withoutX("Hexllo") → "Hexllo"	"Hexllo"	OK	    
other tests

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40592309
str.substring(0,1) does not make sense when str is ""
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592316
and also below two does not make sense right
str.substring(strLen-1);


str.substring(1,strLen-1);

so the corner case scenario right?
let me try to fix that corner case
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40593087
>> and also below two does not make sense right
Why do you think they don't?

>> let me try to fix that corner case
All right. Show us how you did that.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40594342
>> and also below two does not make sense right
Why do you think they don't?
when str is ""  above throw exception as lenght of string is 0.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40594361
All right. Show us how you did that

when i put a if check around to filter out scenaio of length being zero i got error as below
public String withoutX(String str) {

if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+rest+lastChar;


}

}

Open in new window



Compile problems:


Error:	public String withoutX(String str) {
	              ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically 
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value; 
so a value is always returned.

see Example Code to help with compile problems

Open in new window


please advise how to proceed with return statetement same time filtering out zero length strings
0
 
LVL 7

Author Comment

by:gudii9
ID: 40594366
public String withoutX(String str) {
String st=null;
if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
st=firstChar+rest+lastChar;


}
return st;

}

Open in new window


tried like above failing on the same test case but different error
Expected	Run		
withoutX("xHix") → "Hi"	"Hi"	OK	    
withoutX("xHi") → "Hi"	"Hi"	OK	    
withoutX("Hxix") → "Hxi"	"Hxi"	OK	    
withoutX("Hi") → "Hi"	"Hi"	OK	    
withoutX("xxHi") → "xHi"	"xHi"	OK	    
withoutX("Hix") → "Hi"	"Hi"	OK	    
withoutX("xaxbx") → "axb"	"axb"	OK	    
withoutX("xx") → ""	""	OK	    
withoutX("x") → ""	""	OK	    
withoutX("") → ""	"null"	X	    
withoutX("Hello") → "Hello"	"Hello"	OK	    
withoutX("Hexllo") → "Hexllo"	"Hexllo"	OK	    
other tests
OK	    

Open in new window


please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40594373
public String withoutX(String str) {
String st=null;
if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
st=firstChar+rest+lastChar;


}
else{
st=str;
}
return st;

}

Open in new window


now i passed all tests But still i did not understand why i was forced to put else block and assign st to str which seems to me insignificant step. please advise
Expected	Run		
withoutX("xHix") → "Hi"	"Hi"	OK	    
withoutX("xHi") → "Hi"	"Hi"	OK	    
withoutX("Hxix") → "Hxi"	"Hxi"	OK	    
withoutX("Hi") → "Hi"	"Hi"	OK	    
withoutX("xxHi") → "xHi"	"xHi"	OK	    
withoutX("Hix") → "Hi"	"Hi"	OK	    
withoutX("xaxbx") → "axb"	"axb"	OK	    
withoutX("xx") → ""	""	OK	    
withoutX("x") → ""	""	OK	    
withoutX("") → ""	""	OK	    
withoutX("Hello") → "Hello"	"Hello"	OK	    
withoutX("Hexllo") → "Hexllo"	"Hexllo"	OK	    
other tests
OK	    

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 40597811
Don't make it more difficult than needed.
How would you express the corner case?

I think like this:
     If my input string is empty, just return it.

Well in code that reads as:

if (str.length()==0) return str;

Open in new window


If you put that before the code you had in comment with ID: 40592250, you're done.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40599093
public String withoutX(String str) {
String st=null;
if (str.length()==0) return str;
if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
st=firstChar+rest+lastChar;


}


return st;

}

Open in new window


above passed all tests too. How putting as if above is different from putting as else at the end as below

public String withoutX(String str) {
String st=null;
if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
st=firstChar+rest+lastChar;


}
else{
st=str;
}
return st;

}

Open in new window


To me both looks same. Please advise if i any differences, advantages in both of these approaches.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40599441
They are functionally equivalent, although they may differ in how easy it is to follow the logic.
I don't know which is easier for you, but for the java compiler, the first case makes it harder to realize that the =null is unnecessary.  (on the other hand, some might have called the String st unnecessary)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40600919
>> If you put that before the code you had in comment with ID: 40592250, you're done.
You didn't put it before the code you posted in comment with ID:40592250 but in ID:xxxxxxxx

If you have
if (str.length()==0) return str;

Open in new window

as first instruction, you don't need
if(str.length()>=1) {

Open in new window

anymore. Do you see/understand that?

>> They are functionally equivalent
Indeed.
But imo it often makes your code much more readable/understandable if you process the corner cases as soon as possible (ie. at the beginning)
0
 
LVL 84

Expert Comment

by:ozo
ID: 40600944
Without the unneeded
if(str.length()>=1) {
to confuse it, the java compiler would longer have difficulty realizing that the =null is unnecessary.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40603801
anymore. Do you see/understand that?
i see the good point here. If i take care as if (str.length()==0) return str; then i can get rid of other if loop which implyies same thing implicityly

public String withoutX(String str) {
String st=null;
if (str.length()==0) return str;

		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
st=firstChar+rest+lastChar;





return st;

}

Open in new window

above passed all tests
0
 
LVL 7

Author Comment

by:gudii9
ID: 40603804
i see point of handling corner cases at the beginning..

Without the unneeded
if(str.length()>=1) {
to confuse it, the java compiler would longer have difficulty realizing that the =null is unnecessary.

I was not clear on this. Can you please elaborate on this.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40604308
http://codingbat.com/prob/p151940
has a compile problem with
public String withoutX(String str) {
    String st/*=null*/;
    if (str.length()==0) return str;
    if(str.length()>=1){
        int strLen=str.length();
        String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
        String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
        String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
        st=firstChar+rest+lastChar;
    }
    return st;
}
but understands
public String withoutX(String str) {
    String st/*=null*/;
    if (str.length()==0) return str;
//  if(str.length()>=1){
        int strLen=str.length();
        String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
        String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
        String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
        st=firstChar+rest+lastChar;
//  }
    return st;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40604419
public String withoutX(String str) {
String st=null;
if (str.length()==0) return str;

		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
st=firstChar+rest+lastChar;





return st;

}

Open in new window


above passed all tests though without any compilation error with null as well.
please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40604509
Yes, but the =null is unnecessary, and the if(str.length()>=1){ confused java into thinking it was necessary.

BTW, String st is also unnecessary, since
st=firstChar+rest+lastChar;
return st;
could just as easily have been
return firstChar+rest+lastChar;

Things unnecessary for the logic may still be useful for clarity, but it seems to me that things that confuse java can also have potential to confuse programmers.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40605095
>> i see the good point here.
Fine!
0
 
LVL 7

Author Comment

by:gudii9
ID: 40605751
Things unnecessary for the logic may still be useful for clarity, but it seems to me that things that confuse java can also have potential to confuse programmers.

Open in new window

i agree
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40605885
Thanx 4 axxepting
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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