Solved

removing x challenge

Posted on 2014-11-09
25
109 Views
Last Modified: 2014-11-20
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
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
  • 13
  • 7
  • 5
25 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40431914
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
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 250 total points
ID: 40432030
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
 
LVL 7

Author Comment

by:gudii9
ID: 40433114
  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
Industry Leaders: 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 7

Author Comment

by:gudii9
ID: 40435933
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
 
LVL 27

Expert Comment

by:dpearson
ID: 40436562
(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
 
LVL 7

Author Comment

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

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

by:ozo
ID: 40444182
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
 
LVL 27

Expert Comment

by:dpearson
ID: 40445983
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
 
LVL 7

Author Comment

by:gudii9
ID: 40446173
If first and last even though x include.
Where we are giving above instruction
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40446356
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
 
LVL 7

Author Comment

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

Author Comment

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

Author Comment

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

Expert Comment

by:ozo
ID: 40448962
i == 0 && i == str.length()-1
can only be true when str.length() is 1
0
 
LVL 84

Expert Comment

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

Author Comment

by:gudii9
ID: 40450906
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
 
LVL 27

Expert Comment

by:dpearson
ID: 40451074
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
 
LVL 7

Author Comment

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

Expert Comment

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

Author Comment

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

Author Comment

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

Expert Comment

by:ozo
ID: 40456369
if( i == 0 ){
     if( i == str.length()-1 ){
          if( !letter.equals("x") ){
               sb.append(letter) ;
          }
     }
}
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 52
swing controls 2 21
java mysql insert application 14 41
jsp error 6 43
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

763 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