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

Fung How Lim
Fung How Lim
2,402 Points

Questions about ageAsString & Parse.Int

Hi Guys,

Quick question about this video;

  1. Why do we String ageAsString instead of String age?

  2. What was the reason to include integer.parseint to the "int"? Did it mean that the system would not have worked without parse.int?

Very very confused.. would really appreciate the help as I would like to understand it completely before moving forward. cheers.

Thanks! :)

6 Answers

Hiya,

Yes, the name intAsString is the name of a variable. Variables are used to store values. In this case, the value it stores is the string being read in from console.readLine() which requests user input via the keyboard. The user is asked for their age so, hopefully, they type in a number in response to that. This number is stored as a string, though, not a mathematical object. That's because of the way readLine works.

For this exercise, we want to be able to carry out mathematical operations on the user's age; we want to see if they are under 13, or not. We can't do that with a string so we must convert the contents of the variable from a string into a number. The class method parseInt can do this for us. This method takes a string object and parses it, seeing if the contents can be translated easily into an integer. There are a range of methods that do similar things; check out the documentation here.

Once parseInt has done its work on ageAsString, the value that results is then stored in the variable age which is of type int. This is a number and can, therefore, be compared to other values to see if it is greater or smaller, enabling the comparison that is required here to take place.

So, ageAsString is a variable name. This variable holds a value. The name is just for our use; it is named so that it's content is clear - it holds the age entered by the user as a string object. We could equally, but less clearly, name the variable x and it would still hold the same value, that's determined by the user's input, not the variable's name. The variable reserves an area in memory large enough to hold the data type it is assigned to. (For Strings that's not quite true as there can be some very long ones ... the memory is referenced on the fly, but for something like an integer or boolean, the memory may be referenced straight away). We access this memory by using the variable name - that variable allows us to access the data that's held in memory whether that's a string or int, or whatever - the name let's us get at the value and perform operations on it.

Using our example and taking it a little further, let's get two sets of user input:

String userName = console.readLine("What is your name?: ");
String ageAsString = console.readLine("How old are you, %s?: ", userName);

So now we have two variables holding user input. The first is the name and the second is the age. Both are strings. One holds "Steve" and the holds "21". :wink: We can call all the methods that string objects allow on both variables and values. They are both string types even though one holds just numeric characters; they are just that, characters. We couldn't perform any mathematical operations on "21" any more than we could on "Steve" - that makes no sense. "Steve" divided by 7 is what, exactly?! Same as "21" divided by seven is equally nonsensical. But once we've run the parseInt() method and passed ageAsString as a parameter, we can store that in another variable, as above:

int notMyAgeReally = Integer.parseInt(ageAsString);

Now, the variable named notMyAgeReally holds the value of my ficticious age, 21 - a number. This can be divided by seven, or whatever fantastic mathematical operation we want to do; say it's my birthday:

notMyAgeReally = notMyAgeReally + 1;
// or
notMyAgeReally++;

I digress ...

I hope that helps,

Steve.

Hi there,

The ageAsString name is just that; a name. It is descriptive too, so we know exactly what it holds. Calling it just age is fine, too. We can call that variable anything at all but it is best to use descriptive names to avoid any confusion.

The next line takes that variable, ageAsString and converts it into an integer. This uses the class method parseInt(). That takes a string variable and returns an integer. It's like a converter. parseInt is a class method, so we don't need an instance of Integer to use it; we call it straight from the class. But don't worry about that. The format Integer.parseInt(numberStoredAsString) is what is key here.

That last line:

int age = Integer.parseInt(ageAsString);

... this creates a new integer variable (the left side of the equals sign) then converts the user input, ageAsString, into a number format and stores that in the age integer variable.

Does that help your understanding?

Steve.

Fung How Lim
Fung How Lim
2,402 Points

Hi Steve,

Thanks for the explanation. let me recap everything you just said so i know i'm getting this correctly..

ageAsString is a name, or a sequence of letters with a value. to include ageAsString in int, we will need Integer.parseInt to convert this sequence of letter to a number. once this is converted, then it is equivalent to value of "age"?

Steve, excellent answer.

I am assuming that for this exercise that Craig wanted to use two variables. One as a string and the other as an int to drive home the point on how to parse String's to Int's. However, this line could be used to simplify the process.

  int userAge = Integer.parseInt(console.readLine("Enter your age: "));

Because console.readLine() takes a string as input, we can use it as the parameter to pass to Integer.parseInt() and assign the value to ' int userAge '. We could also use a try / catch statement but that will likely come in later videos. Let me know if that helped or if you needed any further clarification.

Fung How Lim
Fung How Lim
2,402 Points

Hi Steve,

thank you so much for the thorough answer on Integer.parseInt(x). No, you could digress all you want as the additional info and examples you provided were really really useful to my learning. I have come to realize this is very similar to mathematics, as you need to replace the value of a line of code with the following line e.g. String x = console.readLine("how old are you? "); [user prompted to input age, which is "x"] and then x is inputted to int age = Integer.parseInt(x); so it's converted into a number because of Integer.parseInt.

Thanks Steve. Really appreciate your help on this. Hi Michael! Thanks for joining in too!

Thanks a dozen guys! Cheers! :)

Perfect - you've got it exactly right. :+1:

Good work! :smile:

Steve.

Daniel Bello
Daniel Bello
635 Points

Thank you for the Clarification Steve Hunter

No problem! :+1: :smile: