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
Joe Leppert
Courses Plus Student 597 Pointsparse.Int();
Hey guys,
I just finished Java Basics. I know the form of parse.Int(); but I am I am not understanding WHY to do it. I tried watching the video several times, it just wasn't clicking. Can someone explain why and when we use this in a different way?
1 Answer
Marco Fregoso
576 PointsYou can use the method Integer.parseInt() to convert a string into an integer value. You have to remember that in programming the value: 10 and "10" are no the same, they might look like they are the same thing but if you were to make some basic math calculations you will get an error from the console. if you try to do
String number = "10";
System.out.println(number*10);
You would get some kind of error since the first value is a string and not a number value. You have to take into consideration that the string named number has a value that right now looks like a number but a string could be anything, just imagine trying to do this
String number = "cake";
System.out.println(number*10);
See? Imagine you were to type the word "cake" into your calculator and then trying to multiply it by 10 it doesn't make sense. But if instead you were to do:
String number = "10";
int newNumber = Integer.parseInt(number);
System.out.println(newNumber*10);
You would get the integer value 100 printed to the console.
Joe Leppert
Courses Plus Student 597 PointsMarco,
Thank you my good Sir! It makes total sense now. I appreciate the quick response.!
Joe Leppert
Courses Plus Student 597 PointsJoe Leppert
Courses Plus Student 597 PointsMarco,
Thank you my good Sir! It makes total sense now. I appreciate the quick response.!