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 trialAndrei Oprescu
9,547 PointsWhat 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.
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
Treehouse TeacherHi 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!
Anuj Bhusari
12,240 PointsIt returns an int and not a String change the return value from string to int and you are good to go
Prince Chinyadza
6,333 Pointspublic 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();
} }