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

Juan Prieto
Juan Prieto
976 Points

Could you guide me without giving me the answer? What Am I doing wrong?

I don't know if I'm using "while" properly.

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 int getRemainingCharacterCount(){
        int count = 0;
      while( MAX_CHARS >= text.lenght()){
           count ++;
      return count;
    }

    }
}

2 Answers

Doli Harahap
Doli Harahap
6,246 Points

What are you trying to do here?

public int getRemainingCharacterCount(){ int count = 0; while( MAX_CHARS >= text.lenght()){ count ++; return count; } }

  1. Your return statement is inside the while, which of course will return 1.
  2. Your while condition is never ending looping.

What do you want to achieve?

Juan Prieto
Juan Prieto
976 Points

Ok so this is how twitter works. 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.

Thats what I basically have to do. Could guide me?

Doli Harahap
Doli Harahap
6,246 Points

ok. First, you text.length got typo.

second, why do you need to use while loop? is that a requirement? I thought you just want to get the remaining text length available, right?

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

or am I missing something? I paused my subscription, so I can't look at the challenge you do.