Solved

string without first and last character challenge

Posted on 2014-11-11
26
112 Views
Last Modified: 2014-11-21
Hi,

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);
  
}

Open in new window


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
0
Comment
Question by:gudii9
  • 15
  • 11
26 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40435425
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;
  
}

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.StringIndexOutOfBoundsException: String index out of range: -1 (line number:5)"      X         
withoutEnd("Chocolate!") → "hocolate"      "hocolate"      OK         
withoutEnd("kitten") → "itte"      "itte"      OK         
withoutEnd("woohoo") → "ooho"      "ooho"      OK         
other tests
OK       

please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40435439
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.substring(2,i-1)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435688
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)

Open in new window

i am still thinking to write in better way
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Author Comment

by:gudii9
ID: 40435693
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 "";
}
  
}

Open in new window


i modified as above and passing all tests. Does it look fine? please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40435744
What you wrote works.
But there is a simpler way to do it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435879
But there is a simpler way to do it.
can you please let me know?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40436532
If you simplify
str.substring(1,2)+str.substring(2,i-1);
then some other complications may also become unnecessary.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40437367
If you simplify
str.substring(1,2)+str.substring(2,i-1);
how to simplify. i am not getting better idea. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40437438
How did you choose the number 2?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40438176
return a version without the first

In the challenge they said as above without first character so i choose 2 to start from 2nd
0
 
LVL 7

Author Comment

by:gudii9
ID: 40438178
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 "";
}
  
}

Open in new window


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?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40438890
On the right track.
Would
str.substring(1,i-1);
work in any other case besides
if(i>2){
?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40441184
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441197
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

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
true

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
true
0
 
LVL 7

Author Comment

by:gudii9
ID: 40441225
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441271
What is the length of substring(1,i-1) ?
What is 1-1?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40441441
Length I am thinking as 0
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441679
yes
0
 
LVL 7

Author Comment

by:gudii9
ID: 40442638
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40443583
the
it looks string starting index 1 till 1 excluding 1
part is true,
the
will not work
part is false

What is the length of the string you want to return if i==2?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40443616
What is the length of the string you want to return if i==2?
0
0
 
LVL 7

Author Comment

by:gudii9
ID: 40443618
so it simply prints empty string if i==2?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40443638
As required.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40443668
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40456126
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
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40456334
Yes, if i=2 str.substring(1,i-1) would be ""
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 65
Apps blocked by Java 9 79
How to execute a Python program and gather return output in Java 2 25
throw exception 21 42
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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…
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 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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now