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 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,694 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:");