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

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Using Parse integers instead of previously used int

In the beginning of the video, it was mentioned that the previous method of using readLine which returns a string was not recommended as int was the correct method of ascertaining integer based information. I'm still not entirely sure why that is the case. Could someone please explain preferably with an example to show why. Thanks

7 Answers

Stephen Bone
Stephen Bone
12,359 Points

Hi Valeshan

The readLine method only returns a string but we need to be able to compare two numbers, in this example whether the user is over 13.

So we need to convert the string value returned from the readLine method to a number or in programming terms an int, which just specifies that the value held in the variable age is a whole number.

The link below goes into more detail regarding primitive types such as int.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Hope that helps!

The confusion for me is that Craig (the user) did insert a number. I understand that readLine only accepts strings. Can we not use another statement that accepts an int?

The readLine() will accept whatever input (Called TEXT) you insert via your keyboard. It can be letters in Chinese "我爱你" or Spanish "¡Hola!"... or digits "000100300200456: .. or symbols "#$%^,.*&". In ALL these cases (and in ANY case actually), the readLine() accepts that input as a string. Thus, inputting "-12" will become the the string "-12", and not the actual integer (-12) The string "-12" is a combination of "-" AND "1" AND "2" ..... and not a number (-12)

So to answer your comment "Craig (the user) did insert a number."

No, Craig did not insert a number. He inserted the string "15" Which he later converted into an integer with the value 15.

Hope this helps, Sam.

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Actually, I think I figured it out. The readLine method doesn't request any user input, so the output is automatically either a positive or negative condition based on the coder's input of the integer. By being able to take an int from a string, you are able to take information from the user to determine whether they meet the requirements of a positive or negative condition.

Lisa Steendam
Lisa Steendam
6,825 Points

int = Integer basically. If you need to access Integer based information it is better to choose a method that returns ints or Integers instead of a string that you will have to parse anyway.

Yaan Batho
PLUS
Yaan Batho
Courses Plus Student 656 Points

Well this video just completely fried my brain :/

I think I understand it, took me a minute though, let me know if I'm right. the var he set as ageAsString w/ the readLine command prompts the user to input their age, but readLine returns their answer as a string. However, our var for age is set as an int datatype and the input won't be read correctly unless the "ageAsString" is converted into an int so the "if" condition can either prompt the program to continue or execute the exit command.....Did i get it?! :D

Adam Couchman
Adam Couchman
892 Points

I think Shmuel Reznik's response to Brad Massengale above helped me to understand this best although it would be great if someone knowledgeable could confirm whether or not I have understand this correctly.

The readLine method will accept whatever input you insert via your keyboard but it will always see that input as text. So typing 13 won’t be read as the number 13, instead it will be read as the character 1 followed by the character 3.

Typing “13” is no different from typing “ab“. Instead of a number it is simply two consecutive characters that happen to be 1 and 3.

The reason that you parse the integer i.e. convert the variable from the String type to the int type is so that the age that the user enters when prompted "How old are you? " can be read as a whole number (integer means whole number) rather than two randomly typed consecutive characters.


I think the video could have done a much better job of explaining this. It took me about 10 minutes of reading and searching to find this thread and then another 10 minutes of pondering and "scribbling" notes in a word document to understand what was going on.

I passed the test at the end of the "Parsing Integers" video very quickly and easily, but at that point I still had no idea why someone would even bother parsing an integer or what it did for them.

Please don't take this comment as a complaint, merely feedback. I'm just really happy that I think I understand it the concept now.