"Java Objects (Retired)" was retired on January 31, 2017. You are now viewing the recommended replacement.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Creating the MVP Strings and Chars

Shubham Modi
Shubham Modi
5,799 Points

I am super confused

Can you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if it doesn't. Thanks!

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }

    public void addTile(char tile) {
      mHand += tile;
        // Adds the tile to the hand of the player

    }

    public boolean hasTile(char tile) {
      if( mHand.indexof("") == 1)
      {
        return true;
      }
      else
      {
       return false;
      }
    }
}

10 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

Ruud - I tried the code you have for hasTitle() method again and it passes. Here's the complete code:

public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }

    public void addTile(char tile) {
        mHand += tile;

    }

    public boolean hasTile(char tile) {
      if(mHand.indexOf(tile) >= 0) { 
        return true; 
      } 
      return false;
    }
}

Is this exactly what you have, including the code for the other methods?

Kourosh Raeen
Kourosh Raeen
23,733 Points

You can use the indexOf() method of the String class. Pass in a character, in this case tile, to the method and it returns -1 if the string does not contain the character and if it does contain the character it returns its index within the string.

So call indexOf on mHand and pass in tile as an argument. Use an if statement and based on what indexOf() returns decide whether hasTitle() should return true or false.

Ruud Visser
Ruud Visser
3,140 Points

Hi Shubham Modi,

First of all, sorry if my comment feels like a thread hijacking. I’m facing the same exercise problem. The clear explanation given by Kourosh Raeen, should fix your problem. In my case I did the following:

public boolean hasTile(char tile) { if(mHand.indexOf(tile) >= 0) { return true; } return false; }

The only problem is I’m getting an error stating: “Hmmm, remember that the indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1”. The use of the >=-characters should prohibit this error from ocurring in the first place? Maybe Kourosh Raeen, or sombody els can explain this error? All other option's I did resulted in above error.

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Ruud - I just tested your code and it passed. Give it another try and make sure you are using >= and not just >.

Ruud Visser
Ruud Visser
3,140 Points

Hi Kourosh Raeen,

I tested my code again while using >= and the result is the same, the error remains. Strange that my code passed in your workspace! I tried some other coding examples from these urls: https://teamtreehouse.com/community/bummer-hmmm-remember-that-the-indexof-method-on-strings-returns-1-if-the-char-is-missing-or-greater-than-that-if-it

https://teamtreehouse.com/community/java-objects-strings-and-char-objective-i-cant-seem-to-get-the-hastile-method-to-work

All the mentioned coding examples are giving the same error in my workspace. I’m out of ideas :-)

Kourosh Raeen
Kourosh Raeen
23,733 Points

Can you post your code please?

Ruud Visser
Ruud Visser
3,140 Points

My code is the same example as posted before:

public boolean hasTile(char tile) { if(mHand.indexOf(tile) >= 0) { return true; } return false;
}

Is it possible that I need to explicit check on -1 ?

Ruud Visser
Ruud Visser
3,140 Points

Kourosh Raeen,

The problem is fixed. The code ran this time! My problem was in the code of the first exercise. Instead of "mHand += tile;" I had the code "String = addToHand String.valueOf(tile); mHand = addToHand; "

The funny thing is, the code did except my input but the problem lies in my interpretation of the exercise. In the first excersices I thought that to add up something means putting in, but no it means counting up! I mixed up the meaning of the word, whoops. Thank you for posting your answer, now I finally know where I went wrong :-) !!

Kourosh Raeen
Kourosh Raeen
23,733 Points

Glad you figured it out! Good luck with the rest of the course!

Furqan Mujahid
Furqan Mujahid
3,367 Points

Can you please the full code? i am completely near but still it doesnt accept. i dont know whats wrong.

Furqan Mujahid
Furqan Mujahid
3,367 Points

Can you please the full code? i am completely near but still it doesnt accept. i dont know whats wrong.

Furqan Mujahid
Furqan Mujahid
3,367 Points

Can you please the full code? i am completely near but still it doesnt accept. i dont know whats wrong.

here is another example of using indexOf

public class ConferenceRegistrationAssistant {

  public int getLineFor(String lastName) {
    /* If the last name is between A thru M send them to line 1
       Otherwise send them to line 2 */
    int line = 0;
    String ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if(ABC.indexOf(lastName.charAt(0)) <= 12){
      line = 1;
    }else{
      line = 2;   
    }
    return line; 
  }
}