-4

Given a string, return true if the first instance of "x" in the string is immediately followed by another "x". It will not work for the case of doubleX("axxbb"). It should return true but gives false.

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

    }

  }
  return false;
}
Gray
  • 115,027
  • 24
  • 293
  • 354
Katy4u
  • 11
  • 1
  • 2
    http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – pvg Mar 30 '17 at 19:51
  • 2
    1. Learn how to use a debugger 2. [`String#substring`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)) does not return a character – UnholySheep Mar 30 '17 at 19:51
  • `String.substring()` returns the *substring* starting at the given index. Generally speaking, that string contains more than one character. It looks like you want the `charAt()` method instead (which returns a `char`, maybe `'x'`, not a `String`). Additionally, it is rarely correct to compare strings with `==`, and you'll need to make sure to avoid trying to access past the end of the string. – John Bollinger Mar 30 '17 at 19:52
  • Terrible title. Please edit to actually describe your issue. – Basil Bourque Mar 30 '17 at 20:33
  • What would you have me change the title to? – Katy4u Mar 30 '17 at 21:37

4 Answers4

1
boolean doubleX(String str){
  if (str == null){
    return false; //defensively check that the input is not null
  }
  int firstX = str.indexOf("x"); 
  if(firstX >=0 && firstX < str.length()-1){  //this ensures that we actually found an x and that there is at least 1 more character after it
     return str.charAt(firstX+1) == 'x'; //return true if the next char is an x
  }
  return false;  //otherwise return false

}
Chris
  • 22,923
  • 4
  • 56
  • 50
0

str.substring(i) doesn't do what you think it does - it returns a substring, not character number i.

Use str.charAt(i) instead.

Malt
  • 28,965
  • 9
  • 65
  • 105
0

The substring method gives you the substring of i to the end of the string. For instance:

str = "test string";
System.out.println(str.substring(3));

Would give you a string from 3 to the end of the string ("t string")

Instead of substring use charAt: https://www.tutorialspoint.com/java/java_string_charat.htm

0

It will not work for the case of doubleX("axxbb"). It should return true but gives false.

You have couple problems with your code:

if (str.substring(i) == "x") {

The String.substring(...) method returns the portion of the string that starts at position i. To quote the javadocs:

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

So if you do str.substring(1) where str is "axxbb" then substring would return "xxbb". I think you should instead use str.charAt(i) which returns the character at that position. So your code should be:

if (str.charAt(i) == 'x') {
    ...
}

The 2nd conceptual problem is that strings cannot be compared with ==. String is an object and when objects are compared with == then you are comparing the reference of the object instead of its contents. To compare strings you should use string.equals("x"). See this answer for more details.

But, again, you wanted to do str.charAt(i) here.

Lastly, your code is going to have problems on the last character because you are string to do a str.charAt(i + 1) which will throw a StringIndexOutOfBoundsException. So the loop should be:

// subtract one from the length because we are looking for 2 x's in a row
for (int i = 0; i < str.length() - 1; i++) {

We go to str.length() - 1 because we are looking for 2 characters in a row.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354