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

Parsing Int

Why can't we just use int age = console.readLine("Enter you're age: "); Instead of: String ageAsString = console.readLine("How old are you? "); int age = Integer.parseInt(ageAsString);

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! It's because any data coming in from the prompt is coming in as a string (and this is also true of other languages besides Java). And you would be trying to assign that string to an integer variable which would cause an error. So we read in the age which looks like a number to the person writing it, but is actually a string. Then we convert that number into an integer.

Hope this helps! :sparkles:

Great. So, in order to go from one data type to another, parsing is required?

Sandy Woods : Yep, parsing is a way (most likely not the only way) to move data from one type to another as needed.