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 trialGreenie 99
1,715 PointsWhy do I get an IllegalArgumentException here?
Hi,
I'm not sure what's wrong with my code. I'm returning the length of the current text in characters subtracted from 140, which would give me the characters left, right?
Could somebody kindly assist.
Thanks,
Greenie 99
public class Tweet {
private String mText;
public static final int MAX_CHARACTERS = 140;
public Tweet(String text) {
mText = text;
}
public String getText() {
return mText;
}
public int charactersLeft(String mText) {
return 140 - mText.length();
}
}
4 Answers
Steve Hunter
57,712 PointsYou can't pass in an argument named the same as the member variable - hence the IllegalArgumentException
.
Since your method can access the member variable, you don't need the parameter. Also, USE YOUR CONSTANT!! lol.
Just try:
public int charactersLeft() {
return MAX_CHARACTERS - mText.length();
}
Greenie 99
1,715 PointsThanks! That makes sense, but I didn't realise that the compiler would take any of that into account.
Pedro Cabral
Full Stack JavaScript Techdegree Student 23,916 PointsThe new method shouldn't take any parameters, you should remove the (String mText) because that is already stored as an object property.
Greenie 99
1,715 PointsThank you. That makes sense!
Izhar Ashraf
762 PointsDeclare a variable that is named the camel-cased version of "first name". Store the user's first name into this new variable using console.readLine.
// I have imported java.io.Console for you. It is a variable called console
public class IO{
public static void main(String[] args){
Console console = System.console();
String firstName = console.readLine("What is your name?");
Console.printf("hello my name is %s\n",firstName);
}
}
//When i am running this code it shows always errors i don't know what to do please help me
Steve Hunter
57,712 PointsHI Izhar,
You have written too much code!
Firstly, you don't need the main
method and secondly, the system console is already provided for you in a variable called console
.
To complete that part of the challenge you just need the line of code you have written:
String firstName = console.readLine("What is your name?");
Let me know how you get on.
Steve.
Izhar Ashraf
762 PointsIzhar Ashraf
762 PointsDeclare a variable that is named the camel-cased version of "first name". Store the user's first name into this new variable using console.readLine. // I have imported java.io.Console for you. It is a variable called console public class IO{
public static void main(String[] args){
Console.printf("hello my name is %s\n",firstName); } }
//When i am running this code it shows always errors i don't know what to do please help me