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 trialJoseph Davis
Courses Plus Student 2,022 PointsWhy do you need to convert a String into an int if you enter an int? int answer=Integer.parsel(answerToLife);
Why do you need to convert a String into an int if you enter an int? int answer=Integer.parsel(answerToLife);
Also, does java recogonise "answer" as separate as long as its lower case and connects them?
String answerToLife = "42";
int answer= Integer.parseInt(answerToLife);
3 Answers
Alexis Beliveau
8,029 Points"42" is of type String. Though it has a number in it, it is still a String data type. In order for the "answer" variable to hold the number 42, you must use Integer.parseInt(answerToLife); This will make "answer" equal to the int parsed from "answerToLife".
Joseph Davis
Courses Plus Student 2,022 Pointsbecause if its an integer why does it need to be a String? I'm sorry, I'm just lost.
Alexis Beliveau
8,029 PointsSo a string is a sequence of characters, the compilers doesn't necessarily care if it has numbers or letters in it. "H3llo" would still be a string. Think of it like characters being strung across the screen. So the answerToLife object is 42, but that's a string. If you were to try to combine answerToLife twice, it would not equal 84, because they are a sequence of characters, and not ints.
I would read the documentation a little
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
And if you don't want to read that here is my very loosely based example!
``` int answerToLife = 42; answerToLife + answerToLife = 84
String answerToLife = 42; answerToLife + answerToLife = "42 42" ```
Joseph Davis
Courses Plus Student 2,022 Pointsint is always mathematical. String is said and literal? Thank you very much. I was so pissed.
Joseph Davis
Courses Plus Student 2,022 PointsJoseph Davis
Courses Plus Student 2,022 PointsSo 42 is an integer but because its being typed in its a String?