Neros E.528 Points
I’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?
-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.
Samuel Ferree31,710 Points
There 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.