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

I am confused on how to interpret this. Does it mean that if a user were to type in 42, the output would b answertolife?

String answerToLife = "42"; int answer = Integer.parseInt(answerToLife);

1 Answer

Kent ร…svang
Kent ร…svang
18,823 Points
// first we declare a variable 'answerToLife' of type String and initialize it to "42":
String answerToLife = "42";

// then we declare a variable 'answer' of type int and initialize it to using a method provided by the
// Integer-class. 'Integer' is an object wrapper class for the primitive variable type 'int', 'int' by itself doesn't
// contain any methods, but the wrapper class have a method called parseInt, which looks inside a string variable
// for numbers, and if there are numbers, then the method can convert the type from a String to an int. 

int answer = Integer.parseInt(answerToLife);

// some examples: 

System.out.printf("%d", answer); 
// outputs --> 42

System.out.printf("%s", answerToLife);
// outputs --> "42"

Hope this helped.

Why does it return quotes when you put answerToLife and not when you put answer? Also, the lesson did not cover system.out.printf, only console.printf