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 Creating the MVP Remaining Characters

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

missing return statement...what i did wrong..?

i think i.m close...but

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


    public int getRemainingCharacterCount(){
    result = MAX_CHARS - text.length();
    if(result>=0){
      return result;
     }
  }

  public String getText() {
    return text;
  }

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

3 Answers

Hello

The problem is here

public int getRemainingCharacterCount(){
    result = MAX_CHARS - text.length();
    if(result>=0){
      return result;
     }
  }


what if the 'if' statement is false?   what is being returned?

you need an else

or you can return some default value, depending on what the challenge is asking you to doo

example

public int getRemainingCharacterCount(){
    result = MAX_CHARS - text.length();
    if(result>=0){
      return result;
     } else{ retun 1}
  }



or

public int getRemainingCharacterCount(){
    result = MAX_CHARS - text.length();
    if(result>=0){
      return result;
     }
return 1
  }



these are just examples to point you into the right direction

if this answers your question, please mark the question as answered.

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

hello, the problem, is there is no need of a if statement, we just need to return a value.. in this particular case, i found out this simple solution:

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

 }

nope ... your code is not correct. you must account for a case when result < 0. What happens then?

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

well i check back my solution, and it works...i mean passed the challenge....so i can not understand, why you said that my code it is worng??, may be not perfect, not the best solution, but works... did you really try my way?.. regards Mark

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

you are right, but on the challenge request, the scope for me, is "return the value" not a validation, as you probably thinking correctly by the way....but for a starting guy like me the challenge it is covered with the small code...