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 trialQuinton Rivera
5,177 PointsHow does the parse method work
Is it the data type of the object that you want the source to be converted into. parse(data type of Original Source(the name of source to be converted)
1 Answer
jcorum
71,830 Pointsshelman, not sure I understand your question.
In this line of code:
int age = Integer.parseInt(ageAsString);
Craig is converting a String ageAsString (e.g., "23") into an int named age (23).
Integer is a final class with static methods, one of which is parseInt().
In a production system you would want to do some error handling in case the String doesn't contain an int.
Amy Sharo
8,567 PointsAmy Sharo
8,567 PointsIf I understand you correctly then yes. We use the Integer.parseInt(source); because we want the source (a String) to be tested (tried) as an integer. If the String is actually a valid number (int), then it has been parsed.