Solved

removing x challenge

Posted on 2014-11-09
25
100 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
  • 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 26

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
 
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 26

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 26

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
DevOps Toolchain Recommendations

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

 
LVL 26

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 26

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
Groovy:unable to resolve class error 2 70
servlet  URL Rewriting 1 27
tomcat not starting 6 31
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

920 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

15 Experts available now in Live!

Get 1:1 Help Now