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

Question about Strings

Hi, I am trying to better understand the different elements of the language and how the different piece fit together.

Is my understanding or what I am saying correct? Can you help me clarify what I don't understand?

String ageAsString = console.readLine("How old are you?  ");
int age = Integer.parseInt(ageAsString);
if (age < 13) {
    //Insert exit code
    console.printf("Sorry kiddo, you must be at least 13 to use this program. \n");
    System.exit(0);
}

String is an object. ageAsString is the method. readLine(""); is an input to return back information for method.

Why is it called "ageAsString" and not "age"?

int age allows me to define age using numbers.

Integer.parseInt is a little more confusing. Integer is an object and therefore it must come before the primitive. I read the documentation but can't quite understand what parseInt means and why were are using it? What is a "decimal integer"?

int age = Integer.parseInt(ageAsString);

can someone help me break down this line ?

I am saying that age can use integers and beyond that I don't understand nor how, really.

if (age < 13) {
    System.exit(0);
}

if age is less than 13, exit the program. How can I call the command using "age" and not "ageAsString"?

Thanks a lot ! Please ask me for clarification!

Hey Jake,

do you need more help?

Grigorij

Hey Jake,

you are so welcome ! See you in the forum !!!

G

1 Answer

Hi Jake,

I understand your confusion.

String ageAsString = console.readLine("How old are you?  ");

This line stores the user input from the console into the variable "ageAsString". The name of the variable can be almost everething you want. Except some characters. The convention, however, is to always begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all for example.

If you type a number into your console, the readLine() method stores this number (same as decimal or int) as String. I know this is confusing but jast remember it. Console input is creating a type String, despite that you tiped a number. So if you want to convert this String input from the console into a real integer, you need to parse/convert it. And you do it with help of this line:

int age = Integer.parseInt(ageAsString);

On the left side you declare a new int and you give it a name age. It can be "myAge" or something else (s. above). On the right side you see an uppercased word Integer. This is a Wrapper. You need wrappers, because they know methods like parseInt() to convert/parse a String into an int. Integer is a wrapper class not an object (also confusing). Inside this class crazy java-geeks defined many methods that primitive types like int can´t have, because they are primitive. You use Wrappers (Integer) to play around with primitives and use fancy methods like parseInt.

So you have converted your String into an int. Now you can use operators like > or < to define conditions.

If your real age is less then int "age" please do something like using a print method "Hey Kid, you are to young"

Could I help you a little bit???

Grigorij

Thanks a ton !