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 Perfecting the Prototype Parsing Integers

What's wrong with my code?

?

ParsingInts.java
String answerToLifeAsString = console.readLine("42");
int answer = Integer.parseInt("ansewerToLife");
Arnold Rosario
Arnold Rosario
16,357 Points

I am also a newbie so I could be wrong, In your second line, you are trying to parse a string "ansewerToLife", in order to parse the int 42 from the variable you created you must pass in the variable answerToLifeAsString inside of the parseInt method.

The correct answer would be: int answer = Integer.parseInt(answerToLifeAsString);

1 Answer

Gavin Ralston
Gavin Ralston
28,770 Points

You're passing a String with the contents "answerToLife" into a function that is trying to look for an integer to parse. :)

Try passing the variable you just created instead, which should have the integer you're looking for:

String answerToLifeAsString = console.readLine("42");
int answer = Integer.parseInt(answerToLifeAsString);