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 trialeliasfaqs eliasfaqs
320 PointsTHis
a
String answerToLife = "42";
int answer = Integer.parseInt(answeresToLife);
1 Answer
Oliver Michaelsen
Courses Plus Student 1,166 PointsHello,
I'm unsure of what you're asking? If you're wondering how it works, it's pretty simple. You created the string answerToLife and set it to 42. String is a data type in Java and so is int, but they're two different data types so directly assigning answerToLife with answer would cause an error since you can't assign two different data types to each other. That's where parseInt comes in handy, it will convert the string to an int. The line int answer = Integer.parseInt(answeresToLife); will simply take the string "42" stored in "answersToLife" and convert the string to int and store it in answer. As an example, you could write int example = answer + 10; and answer will now be treated as an int and the new int called example would now store 52.
If that wasn't what you needed and you're asking for help because you're encountering an error, you misspelled your string when you were trying to parse it.
String answerToLife = "42";
int answer = Integer.parseInt(answerToLife); //You spelled it answeres.
eliasfaqs eliasfaqs
320 Pointseliasfaqs eliasfaqs
320 PointsThank you,but I just figured out how to do this!