Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Remaining Characters

Andrei Oprescu
Andrei Oprescu
9,547 Points

What is wrong?

I have this question: Create a public method named getRemainingCharacterCount that returns an int representing how many characters they have left before they 140. Base your calculation on the field that stores the current text.

And I have this code:

public class Tweet { private String text; public static final int MAX_CHARS = 140;

public Tweet(String text) { this.text = text; }

public String getText() { return text; }

public void setText(String text) { this.text = text; }

public String getRemainingCharacterCount() { return MAX_CHARS - text.length();

} }

I keeps giving me an error that in line 18 (return MAX_CHARS - text.length();) the second variable is a String and I don't know why.

Can you please help me?

Andrei.

Tweet.java
public class Tweet {
  private String text;
  public static final int MAX_CHARS = 140;

  public Tweet(String text) {
    this.text = text;
  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  public String getRemainingCharacterCount() {
  return MAX_CHARS - text.length();

  }
}

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great! But you've confused Java a little bit here. When we write a function definition we specify what data type we're returning or void to return nothing. You're returning an int which is exactly what you should be returning, but you told Java that you're going to return a String.

You typed:

  public String getRemainingCharacterCount() {

But you meant to type:

  public int getRemainingCharacterCount() {

This tells Java that it can expect a data type of int to be returned from that function. That's why you're getting the error about not being able to convert between the data types.

Hope this helps! :sparkles:

Anuj Bhusari
Anuj Bhusari
12,240 Points

It returns an int and not a String change the return value from string to int and you are good to go

public class Tweet { private String text; public static final int MAX_CHARS = 140;

public Tweet(String text) { this.text = text; }

public String getText() { return text; }

public void setText(String text) { this.text = text; }

public int getRemainingCharacterCount() { return MAX_CHARS - text.length();

} }