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 is wrong with this code

String firstName = console.readLine("What is your name? "); String lastname = "Dangol" String lastname = console.readLine;

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

String firstName = console.readLine("What is your name?  ");
String lastname = "Dangol"
String lastname = console.readLine;

3 Answers

Jeff Wilton
Jeff Wilton
16,646 Points

Delete your second line, and make the lastName var do something similar to your firstName var.

String firstName = console.readLine("What is your first name?  ");
String lastName = console.readLine("What is your last name?  ");

Hi,

You need to use "console.readLine ()" to get the lastName from the user.

Here's the solution :

String firstName = console.readLine("What is your First Name?  ");
String lastName = console.readLine("What is your Last Name?");

Challenge wants you to create a variable lastname in camelCase like this "lastName".

and then to use console.readLine() method just like you used to get firstName , to get the lastName in this part.

console.readLine() will print whatever you wrote in the parentheses("What is your Last Name") on the console and will wait for user to enter anything , and will return whatever user typed in as his last name. So we store that returned value in a string named "lastName".

I hope it answers your question, Happy Coding :)

  • Zain
Fathima Fas-ha
Fathima Fas-ha
6,847 Points

You cant create two variables with same name and same type. String lastName="Dangal";//in this line you've missed a semicolon String lastName =console.readLine();//missing parentheses. both statements at once wont compile,it may cause compile error variable lastName is already defined ,therefore select one statement according to your need.