gudii9
asked on
string without first and last character challenge
Hi,
I am trying below challenge
http://codingbat.com/prob/p130896
i worte as below
i am getting error as below
Compile problems:
Error: public String withoutEnd(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.
please advise on how to fix and improve my code. Thanks in advance
I am trying below challenge
http://codingbat.com/prob/p130896
i worte as below
public String withoutEnd(String str) {
int i=str.length();
if(i>=2)
return str.substring(1,2)+str.substring(2,i-1);
}
i am getting error as below
Compile problems:
Error: public String withoutEnd(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.
please advise on how to fix and improve my code. Thanks in advance
What is "ab" without the first and last character, and how can you return it without giving substring an out of range index?
You might also think about whether there is a better way to write
str.substring(1,2)+str.sub string(2,i -1)
You might also think about whether there is a better way to write
str.substring(1,2)+str.sub
ASKER
What is "ab" without the first and last character, and how can you return it without giving substring an out of range index?
without first and last character "ab" is nothing so i got above error which make sense now.
"The string length will be at least 2. "
Above line given in problem challenge. Does that mean 2 is included or no(i mean string leght should be 3 or more)??
You might also think about whether there is a better way to write
str.substring(1,2)+str.substring(2,i-1)
i am still thinking to write in better way
ASKER
public String withoutEnd(String str) {
int i=str.length();
String str2=null;
if(i>2){
str2= str.substring(1,2)+str.substring(2,i-1);
return str2;
}
else
{
return "";
}
}
i modified as above and passing all tests. Does it look fine? please advise
What you wrote works.
But there is a simpler way to do it.
But there is a simpler way to do it.
ASKER
But there is a simpler way to do it.can you please let me know?
If you simplify
str.substring(1,2)+str.sub string(2,i -1);
then some other complications may also become unnecessary.
str.substring(1,2)+str.sub
then some other complications may also become unnecessary.
ASKER
If you simplifyhow to simplify. i am not getting better idea. please advise
str.substring(1,2)+str.substring(2,i -1);
How did you choose the number 2?
ASKER
return a version without the first
In the challenge they said as above without first character so i choose 2 to start from 2nd
ASKER
public String withoutEnd(String str) {
int i=str.length();
String str2=null;
if(i>2){
str2= str.substring(1,i-1);
return str2;
}
else
{
return "";
}
}
I think i got what you meant. I was able to modify as above and able to pass all the tests. Is my code in right track?
On the right track.
Would
Would
str.substring(1,i-1);work in any other case besides
if(i>2){?
ASKER
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will not work as it looks string starting index 1 till 1 excluding 1
if i=1 then str.substring(1,i-1) become str.substring(1,0);//will not work as it looks string starting index 1 till 0 excluding 0
if i=0 then str.substring(1,i-1) become str.substring(1,-1);//will not work as it looks string starting index 1 till -1 excluding -1
if i=1 then str.substring(1,i-1) become str.substring(1,0);//will not work as it looks string starting index 1 till 0 excluding 0
if i=0 then str.substring(1,i-1) become str.substring(1,-1);//will
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will not work as it looks string starting index 1 till 1 excluding 1false
if i=1 then str.substring(1,i-1) become str.substring(1,0);//will not work as it looks string starting index 1 till 0 excluding 0true
if i=0 then str.substring(1,i-1) become str.substring(1,-1);//willtruenot work as it looks string starting index 1 till -1 excluding -1
ASKER
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will not work as it looks string starting index 1 till 1 excluding 1
false
i wonder why it is false.
How ti looks for string starting index 1 till index 1 excluding index 1.
please advise
What is the length of substring(1,i-1) ?
What is 1-1?
What is 1-1?
ASKER
Length I am thinking as 0
yes
ASKER
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will not work as it looks string starting index 1 till 1 excluding 1
false
I wonder why above answer is false. I am still thinking it will not work. please advise
the
the
What is the length of the string you want to return if i==2?
it looks string starting index 1 till 1 excluding 1part is true,
the
will not workpart is false
What is the length of the string you want to return if i==2?
ASKER
What is the length of the string you want to return if i==2?0
ASKER
so it simply prints empty string if i==2?
As required.
ASKER
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will not work as it looks string starting index 1 till 1 excluding 1
i have to correct as
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will work as it looks string starting index 1 till 1 excluding 1
ASKER
i have to correct as
if i=2 then str.substring(1,i-1) become str.substring(1,1);//will work as it looks string starting index 1 till 1 excluding 1
and prints "".
I am correct in my understanding right?
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window
i tried as above still test cases failing
Expected Run
withoutEnd("Hello") → "ell" "ell" OK
withoutEnd("java") → "av" "av" OK
withoutEnd("coding") → "odin" "odin" OK
withoutEnd("code") → "od" "od" OK
withoutEnd("ab") → "" "Exception:java.lang.Strin
withoutEnd("Chocolate!") → "hocolate" "hocolate" OK
withoutEnd("kitten") → "itte" "itte" OK
withoutEnd("woohoo") → "ooho" "ooho" OK
other tests
OK
please advise