Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

x after other x string challenge

Posted on 2014-10-18
27
Medium Priority
?
453 Views
Last Modified: 2014-10-20
I am trying below challenge

http://codingbat.com/prob/p186759

I wrote as below thinking this is perfect code

boolean doubleX(String str) {
  for(int i=0;i<str.length()-1;i++){
  
  if(str.substring(i).equals("X") && str.substring(i+1).equals("X")){
  return true;
  }
}
}

Open in new window


But i got below compilation error. I wonder why i got error saying no return eventhough i am returning true.


Compile problems:


Error:      boolean doubleX(String str) {
              ^^^^^^^^^^^^^^^^^^^
This method must return a result of type boolean

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.

see Example Code to help with compile problems



I modified my code as below by giving some explicit return false(I am not sure what to return so arbitrarily gave false) outside for loop

boolean doubleX(String str) {
  for(int i=0;i<str.length()-1;i++){
  
  if(str.substring(i).equals("X") && str.substring(i+1).equals("X")){
  return true;
  }
  
}

return false;
}

Open in new window


I have some test cases passing some failing as below

Expected      Run            
doubleX("axxbb") → true      false      X         
doubleX("axaxax") → false      false      OK         
doubleX("xxxxx") → true      false      X         
doubleX("xaxxx") → false      false      OK         
doubleX("aaaax") → false      false      OK         
doubleX("") → false      false      OK         
doubleX("abc") → false      false      OK         
doubleX("x") → false      false      OK         
doubleX("xx") → true      false      X         
doubleX("xax") → false      false      OK         
doubleX("xaxx") → false      false      OK         


Please advise on how to fix and improve my code
0
Comment
Question by:gudii9
  • 10
  • 5
  • 4
  • +3
27 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 40388998
Yes, but in the first case, you are trying to return a value from within an if clause - so if the condition doesn't hold, then there will be no return, and that is illegal since you promise to return something in the method signature.

In the second case, if you return an arbitrary false value, what would that achieve, apart from give you the wrong answer now and again?
0
 
LVL 17

Accepted Solution

by:
krakatoa earned 400 total points
ID: 40389102
boolean doubleX(String str) {

  boolean found = false;
  int intpos=0;
  
  if((intpos = str.indexOf("x"))>-1){
        if(intpos<str.length()-1&&str.charAt(intpos+1)=='x')
        {found = true;}
  
  }
  
   return found;
}

Open in new window

0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40389133
See for the fist time I've taken a look at the solution for this challenge, and I personally would say that their return statement is bad practice, even though it works -

return str.substring(i+1, i+2).equals("x");

The signature of the method is a boolean, and so that other people - and later most likely you too - can understand fully what's going on, return should be expressed simply by a finished boolean :

return found;

Others here may disagree, but if this is meant to be a learning exercise, then I'd say that that sort of esoteric and idiosyncratic phraseology is saved for later down the road.
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 85

Expert Comment

by:ozo
ID: 40389167
You are always returning false, because str.substring(i).equals("X") is never true
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40389201
In this question, your mistake was to use a loop - you do not need a loop for this. Think about it -

you have been asked to find if the first occurrence of x is followed immediately by another x. So all you have to do is to find x ONCE, and then ask if the next character after it is also an x. You can find it ONCE by str.indexOf("x")>-1.


- - - -
Footnote.
And let us be clear about this : the "-1" which is returned if x is not found, is not an absolute index position in the way that any positive result (ie 0 or more) would be. The -1 in such a case is a quasi-token for "false" (i.e. "not found"), and has to be an int because that is the return type for the method indexOf().
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 400 total points
ID: 40389216
What krakatoa is suggesting is definitely a good approach, but working from where you started from you need to make a couple of changes.

First str.substring(i) is not what you want.  This returns the substring from i to the end of the string.  
You want str.substring(i, i+1) - so it returns just 1 character.

Also you need to leave the loop after finding the first match.

Something like this:

boolean doubleX(String str) {
  for(int i=0;i<str.length()-1;i++){
  
  if(str.substring(i, i+1).equals("x")) {
    if (str.substring(i+1, i+2).equals("x")){
      return true;
    }
    return false ;
  }
  
}

return false;
}

Open in new window


Does that make sense?

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40389244
boolean doubleX(String str) {

  boolean found = false;
  int intpos=0;
 
  if((intpos = str.indexOf("x"))>-1){
        if(intpos<str.length()-1&&str.charAt(intpos+1)=='x')
        {found = true;}
 
  }
 
   return found;
}

The above code worked fine. What is meaning of below line. Does there is scenario where intpos comes greater than str.length()-1.

 please advise
intpos<str.length()-1
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 40389275
return should be expressed simply by a finished boolean :

return found;

Yes, i think that's clearer. I like your solution but would have done it slightly differently:

        boolean found = false;
        int intpos = -1;

        if ((intpos = str.indexOf('x')) > -1) {
            found = (intpos < str.length() - 1) && (str.charAt(intpos + 1) == 'x');
        }

        return found;

Open in new window


Another

 
       int ixFirstX = str.indexOf('x');

        return (ixFirstX >= 0) && (str.indexOf('x', ixFirstX + 1) == (ixFirstX + 1));

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40389295
In this question, your mistake was to use a loop - you do not need a loop for this.

Is using for loop reduces performance of the java process. Definitely agree the solutions you provided are far better than my original solution.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40389296
int intpos = -1;

what is importance of defining as -1 rather than 0.


(str.charAt(intpos + 1) == 'x')
Instead of above can i say as below. please advise
(str.charAt(intpos + 1) .equals( '"x'");
0
 
LVL 7

Author Comment

by:gudii9
ID: 40389298
Error:      if(pos<str.length()-1&&str.charAt(pos+1).equals("x")){
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cannot invoke equals(String) on the primitive type char

I got a error when i write like above.

I thought 'x' is also string. Cannot i consider it as string with only one character?
Why java does not like it with equals method for 'x' but rather forcing to use ==

Please advise
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40389322
Definitely agree the solutions you provided are far better than my original solution.

Not so much a case of being better, but of being correct. Even then, correctness doesn't equate at all times to optimum efficiency.

I'd say now's your chance to study carefully what the big guns CEHJ and Doug have said about this matter, as again, these guys are waaaaay ahead in their knowledge of Java. Ozo too, and of course as I said mccarl.

You should concentrate on the objective before you decide how to begin to code anything. Good luck.
0
 
LVL 85

Expert Comment

by:ozo
ID: 40389739
While there are advantages to some of the other approaches, to use a loop per se was not necessarily a mistake so much as the tests you made in the loop, and it could still be a useful exercise to try to understand why the results you originally generated were incorrect
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390029
boolean doubleX(String str) {
  for(int i=0;i<str.length()-1;i++){
  
  if(str.substring(i,i+1).equals("X") && str.substring(i+1,i+2).equals("X")){
  return true;
  }
  
}

return false;
}

Open in new window



when i wrote as above some test cases failing as below

Expected      Run            
doubleX("axxbb") → true      false      X         
doubleX("axaxax") → false      false      OK         
doubleX("xxxxx") → true      false      X         
doubleX("xaxxx") → false      false      OK         
doubleX("aaaax") → false      false      OK         
doubleX("") → false      false      OK         
doubleX("abc") → false      false      OK         
doubleX("x") → false      false      OK         
doubleX("xx") → true      false      X         
doubleX("xax") → false      false      OK         
doubleX("xaxx") → false      false      OK         



Whereas below Doug code is passing all test cases
boolean doubleX(String str) {
  for(int i=0;i<str.length()-1;i++){
  
  if(str.substring(i, i+1).equals("x")) {
    if (str.substring(i+1, i+2).equals("x")){
      return true;
    }
    return false ;
  }
  
}

return false;
}

Open in new window



I wonderhow my code is different from above code.Please advise
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40390748
The two programs are very similar.

You do have a small typo - you're testing for capital "X" instead of lower case "x".

But the only real difference is what happens after we find the first x.

In your program the code is essentially this:

if character(i) == 'x' AND character(i+1) == 'x'
   return true;

In my program the code is essentially this:

if character(i) == 'x' then
{
   if character(i+1) == 'x'
       return true ;
   return false ;
}

So can you see how my program will always return a result after it finds the first 'x' in the string?
While yours keeps checking all pairs?

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390815
if character(i) == 'x' then
{
   if character(i+1) == 'x'
       return true ;
   return false ;
}

you program tries to find x at i+1 position only if it finds 'x' in the i position.

where as my program looking for pairs(xx).

But i feel both at the end find xx only right. I am missing what is the difference between these two approaches. Please advise
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40390824
you program tries to find x at i+1 position only if it finds 'x' in the i position.
where as my program looking for pairs(xx).

That's correct, but my program always returns a result (true or false) once it finds that first 'x'.

Yours keeps checking for pairs.  So if the string is:

"xabcxx"

mine returns "false" because it finds the first 'x', checks the next letter ('a') and returns false immediately.

Yours finds the first 'x', sees that the next character is 'a' and then keeps on looking for more matches.
Eventually it finds 'xx' at the end of the string and returns "true".

Which in this case it should not (because of the way the problem was set up).

Make sense?

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390839
It make sense now.

  if (str.substring(i+1, i+2).equals("x")){

Is equals is case sensitive
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390844
My if condition is checking till it find pair of xx and then returning true.
Where as yours goes inside only if it finds x first time and goes inside other if loop and see if is followed by other x then return true.

I read challenge more carefully now which says  first instance of "x" in the string is immediately followed by another "x".


Given a string, return true if the first instance of "x" in the string is immediately followed by another "x".
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40391038
Yep - I think you've got it now.

And yes "equals" is case sensitive.

There's also "equalsIgnoreCase()" that you can use if you want it to be case insensitive.

Doug
0
 
LVL 32

Expert Comment

by:awking00
ID: 40392053
Why not just -
boolean doubleX(String str) {
 return str.indexOf("x") == str.indexOf("xx");
}
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 400 total points
ID: 40392062
Oops. Need to take care of no "x" -
boolean doubleX(String str) {
if (str.indexOf("x") > -1 {
 return str.indexOf("x") == str.indexOf("xx");
}
 return false;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40392575
after -1 i put ) and worked fine
0
 
LVL 32

Expert Comment

by:awking00
ID: 40392582
Sorry about that. Yes the if condition should be surrounded by parentheses.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40392593
return should be expressed simply by a finished boolean
what is meaning of above line. Is it like instead of returning false return corresponding variable name (which in this case found?)
Please advise
0
 
LVL 32

Expert Comment

by:awking00
ID: 40392609
Instead of if (str.indexOf("x") > -1) { , you could also have used -
if (str.contains("x")) {
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 40392644
str.indexOf("x") can have an advantage over str.contains("x"), in that you could save the value and not have to compute it again to compare with str.indexOf("xx"),
but compared with some of the other methods, str.indexOf("x") == str.indexOf("xx") has the disadvantage that it can do a lot of extra work for
doubleX("xaaaaaaaaaaaaaaaaaaaxx")
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

885 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