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 trialNeros E.
528 PointsI’m having a hard time truly understanding the term “Converting a String to an int”. Please help!
After more time than I’d like searching the internet and TTH community I think I’m close to really getting it, can someone confirm if I’m understanding this right?.
I read here that any input received by the user through the console will always be understood as a string (set of letters) and that in order for the TreeStory program to execute the if statement (age <13) which needs a numeral input to function, I will need to convert the “ageAsString” into an Integer using the handing translation tool “Interger.parseInt(ageAsString)”? is this right, am I missing something?
Additional questions:
-When it’s said the console only understands input as string, does that mean it would understands “13” as “thirteen” or something else? -If yes (ignoring the inconvenience) … is “if (age < thirteen )” possible?
Any help is highly appreciated.
1 Answer
Samuel Ferree
31,722 PointsThere is a difference, to a computer, between the character that represents "1" and the number 1. There is a difference between the string of characters "13" and the number 13.
This happens because all data in a computer is stored as a binary number. So the computer needs to know how to interpret a series of 1's and 0's as something else, and how those 1's and 0's are interpreted depends on the type type of data it is.
for instance, to store the number 1 in binary, as a byte, the data is 00000001 but to store the character 1 in binary, as a byte, the data is 00110001
So the computer reads in the user input, which can be anything on the keyboard, as a string of characters
But your program wants to work with numbers. If you were to just used the data that represents the character "1", as a number, the number you would be working with is actually 49. So we need to convert the character "1", to the number it represents to humans. This is known as parsing.
"if (age < thirteen)" technically is possible, IFF you've declared thirteen as variable.
int thirteen = 13;
if(age < thirteen)
But this is not a good way of doing things.
Neros E.
528 PointsNeros E.
528 PointsSo if the user inputs a 1, the computer interprets this number as a string of characters (00110001), that if not parsed will be seen by me as 49 ... because that’s the binary "character" equivalent of 1. In other word I convert the String of characters with “int age = Interger.sparceInt(ageInString);” into the number (int) the user actually meant (00000001).
Character: Any input received by the key board Number : understandable by humans The only thing they have in common is 1 and 0.
Btw Thank you so much for the taking from your time to help with my beginner issue, I know it may be basic material but you have no Idea how much this helps me, especially because I’m trying to really learn and not memorize.