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 Basics Getting Started with Java IO

What am i doing wrong?

I cant understand my mistake with help of preview mode please help. Here's what i had to do:"Declare another variable, naming this one the camel-cased version of "last name". Use console.readLine to store the user's last name into this new variable."

IO.java
// I have imported java.io.Console for you.  It is a variable called console.
String firstName = "Bla";
String firstName = console.readLine("Tom");
String lastName = "Tom";
String lastName = console.readLine("Tom");

1 Answer

Simon Coates
Simon Coates
28,693 Points

you can reassign a value to a variable, but you can't redefine a variable in the same scope.

// I have imported java.io.Console for you.  It is a variable called console.

/*
//this is illegal code as it tries to define an already existing varaible
String firstName = "Bla";
String firstName = console.readLine("Enter a First Name:");
*/

/*
//this would work but is inefficient. It creates the variable, assigns it a value and then overwrites the value.
String firstName = "Bla";
firstName = console.readLine("Enter a First Name:");
*/

/*
//this eliminates the extra assignment.
String firstName; // variable is declared
firstName = console.readLine("Enter a First Name:"); //value is assigned
*/

String firstName = console.readLine("Enter a First Name:"); //declaration and assignment in a single step.

String lastName = "Tom";
lastName = console.readLine("Enter a Last Name:");