removing x challenge

Hi,

I am trying below example
http://codingbat.com/prob/p171260

I wrote my code as below
public String stringX(String str) {
StringBuilder sb=new StringBuilder("str");
for(int i=0;i<str.length()-1;i++)
{
if(str.substring(i,i+1).equals("x"))
sb.delete(i,i+1);
return sb.toString();
}
return sb.toString();
}

Open in new window


Please advise on how to fix and improve the my code. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
You might want sb to be built from the str that is passed in, rather than always using the same "str"
You may also want to make the code work when more than one character should be deleted.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpearsonCommented:
If you're doing a series of deletes (like this function requires), it's hard to use the StringBuilder.delete() method because the character positions in that string are going to be different from the character positions in the string you are scanning.

Say you start with
str = "xabcx"
sb  = new StringBuilder("xabcx") ;

Now if you delete the first character from sb (sb.delete(0)) it changes the position of the remaining characters in the sb string as now it is "abcx".

So all in all, I think it's easier to just add strings to the buffer as you go.

Something like this:

public String stringX(String str) {
StringBuilder sb=new StringBuilder();
for(int i=0;i<str.length();i++)
{
  String letter = str.substring(i,i+1) ;
  if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) ;
}
return sb.toString();
}

Open in new window


Do you see how this code is similar to what you wrote, but only does appends to the StringBuilder at the right time?

Doug
0
gudii9Author Commented:
  String letter = str.substring(i,i+1) ;
  if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) ;

Open in new window


I am not very clear on above two lines. if it first or last or not x then apend to stringbuider sb(is buffer same as string builder..i see you used word buffer in your comment)


what happens if other than below case
if it first or last or not x then apend to stringbuider sb
I see all tests do pass.
Please advise
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

gudii9Author Commented:
You might want sb to be built from the str that is passed in, rather than always using the same "str"
You may also want to make the code work when more than one character should be deleted.

can you please advise on this.
0
dpearsonCommented:
(is buffer same as string builder..i see you used word buffer in your comment)

Yes StringBuffer and StringBuilder are (essentially) identical.  You should generally use StringBuilder.  It's a newer implementation and slightly better than StringBuffer, but both would work.

what happens if other than below case
if it first or last or not x then apend to stringbuider sb

We're looking to build up a new string that contains:
1) The first character
2) The last character
3) Any character that's not an x (between the first and last characters)

So this test
if(i == 0 || i == str.length()-1 || !letter.equals("x"))

does all 3 of those tests in one go.

If any of them are true, we add the character into the string we'll return.
Otherwise, we don't do anything and we just move on to the next character.

Does that make sense?

Doug
0
gudii9Author Commented:
We're looking to build up a new string that contains:
1)  The first character
2) The last character

it should like below right? please advise

We're looking to build up a new string that contains:
1) Not The first character
2) Not The last character
0
gudii9Author Commented:
3) Any character that's not an x (between the first and last characters)

i was clear on above point. But not below points
We're looking to build up a new string that contains:
1)  The first character
2) The last character
please advise
0
ozoCommented:
an "x" at the very start or end should not be removed.
So an "x" at the very start or end should be included.
Also, a non "x" at the very start or end should be included.
So any character at the very start or end should be included.
0
gudii9Author Commented:
 if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) 

Open in new window


can we break to few more lines and simplify above code to exactly see where is beolw things happening clearly

So an "x" at the very start or end should be included.
Also, a non "x" at the very start or end should be included.
So any character at the very start or end should be included.
please advise
0
ozoCommented:
String letter = str.substring(i,i+1) ;
if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter)
i == 0 → letter at the very start
i == str.length()-1 → letter at the very end
!letter.equals("x") → letter a non "x"
These cover all the cases when character should be included
0
dpearsonCommented:
can we break to few more lines and simplify above code to exactly see where is beolw things happening clearly
Sure - you can always break up an if statement into separate lines if you prefer.

So this:

if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) 

Open in new window


becomes this:

if (i == 0)
  // We're at the start
  sb.append(letter) ;
else if (i == str.length()-1)
  // We're at the end
  sb.append(letter) ;
else if (!letter.equals("x"))
  // It's not an 'x'
  sb.append(letter) ;

Open in new window


It's more code, but it does the same thing.
0
gudii9Author Commented:
If first and last even though x include.
Where we are giving above instruction
0
dpearsonCommented:
If first and last even though x include.
Where we are giving above instruction

The problem statement had this:
   >> Except an "x" at the very start or end should not be removed.
0
gudii9Author Commented:
If first and last even though x include.
Where we are giving above instruction

i mean in the code where we are taking care of it
0
gudii9Author Commented:
else if (!letter.equals("x"))
  // It's not an 'x'
  sb.append(letter) ;

in the above code we are saying if no x(in any positions other than first and last) then add to string builder right?


if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) 

Open in new window



do not we should have like below instead of above
if(i == 0 && i == str.length()-1 && !letter.equals("x"))
    sb.append(letter)
0
gudii9Author Commented:
else if (!letter.equals("x"))
  // It's not an 'x'
  sb.append(letter) ;

so whatever comes inside above else if is the i when we are not at first and last index right
0
ozoCommented:
i == 0 && i == str.length()-1
can only be true when str.length() is 1
0
ozoCommented:
so whatever comes inside above else if is the i when we are not at first and last index right
given
if (i == 0)
  // We're at the start
  sb.append(letter) ;
else if (i == str.length()-1)
  // We're at the end
  sb.append(letter) ;
else if (!letter.equals("x"))
  // It's not an 'x'
  sb.append(letter) ;

the cases of the i when we are at first and last index are handled in the first two else statements, so if we get to the last else,  we are not at first and last index
0
gudii9Author Commented:
i == 0 && i == str.length()-1
can only be true when str.length() is 1
our solution we are not using && but using || as below
I wonder what is relevance of && for this solution.
if(i == 0 || i == str.length()-1 || !letter.equals("x"))
0
dpearsonCommented:
For this problem you want an "or" because we add the current character to the result if any of the conditions are true.
You want "and" when all of the conditions need to be true for the character to be added.

Doug
0
gudii9Author Commented:
if (i == 0)
  // We're at the start
  sb.append(letter) ;
else if (i == str.length()-1)
  // We're at the end
  sb.append(letter) ;
else if (!letter.equals("x"))
  // It's not an 'x'
  sb.append(letter) ;

Open in new window

Above if -else if-else if construct resembles to me like 'and' not 'or'. Please advise with some simple example
0
ozoCommented:
The above if -else if-else if construct does sb.append(letter)  when any of the conditions are true.
The || construct also does sb.append(letter)  when any of the conditions are true.

i == 0 || i == str.length()-1 is true when either i == 0 is true or i == str.length()-1 is true.
i == 0 && i == str.length()-1 is only true when both i == 0 is true and i == str.length()-1 is true.
(which can only happen when str.length()==1)
0
gudii9Author Commented:
The above if -else if-else if construct does sb.append(letter)  when any of the conditions are true.
The || construct also does sb.append(letter)  when any of the conditions are true.
now i know how to visualize those if -else if-else constructs, basically inside out, as inside common sb.append(letter)  appends when any one of conditions are met(not all)
0
gudii9Author Commented:
similar to how we break
if(i == 0 || i == str.length()-1 || !letter.equals("x")

as simple steps of  if -else if-else if construct as below
if (i == 0)
  // We're at the start
  sb.append(letter) ;
else if (i == str.length()-1)
  // We're at the end
  sb.append(letter) ;
else if (!letter.equals("x"))
  // It's not an 'x'
  sb.append(letter) ;

Open in new window


how to break into simple steps?
if(i == 0 && i == str.length()-1 && !letter.equals("x")
0
ozoCommented:
if( i == 0 ){
     if( i == str.length()-1 ){
          if( !letter.equals("x") ){
               sb.append(letter) ;
          }
     }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.