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

Why 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

Tweet.java
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();
  }

}

Declare 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

4 Answers

You 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. :+1:

Just try:

  public int charactersLeft() {
      return MAX_CHARACTERS - mText.length();
  }

Thanks! That makes sense, but I didn't realise that the compiler would take any of that into account.

The new method shouldn't take any parameters, you should remove the (String mText) because that is already stored as an object property.

Thank you. That makes sense!

Declare 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

HI 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.