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


Checking to see if I have String to Integer conversion correct in Java?

So I'm one of those type of people who has to be certain that he has things down pat and completely understand how it works and why it works before I move on. So...

String yourAge = console.readLine("How old are you? "); int age = Integer.parseInt(yourAge);

That means that the string above needs a numerical answer as an input correct? by using that method, if the person was to type in, for whatever reason, words. would that crash it like a bad smell? if so is there a way to get your question to be able to be answered with numbers or letters? and if so would it be extra lines of code or would it use something along the lines of a && method?

Also, when I type something like.

String moralCompass = "64";

why have the quotations made it acceptable?

Sorry about the newb questions, I haven't delved that much into coding yet and I'm still very new to this whole thing, I started with Java Basics and I've only just started on Objects but if anyone could clear those questions up for me it would be fantastic! While I can still go ahead and use the code I like to know hows its working and why?

I do get though that.

String answerToLife = "42"; int answer = Integer.parseInt(answerToLife); then takes that number and turns it into an integer, but wouldn't it just be simpler to go.

int answer = 42; ??

I assume there are different applications for this kind of code which is why you would need conversions, could someone give me an example or idea? All of these questions go through my head while I'm doing this hahahahaha. Thank you!

P.S. Sorry about the wall of text!!! :)

2 Answers

That code, if you enter in something other than an integer, will throw a NumberFormatException. If you do nothing to prepare for that possibility, then yes, the program will crash. Later you'll learn how to catch that error with a try / catch loop, and how to respond to users that enter bad input before you continue processing, but I don't think you're there yet. So, for an answer, it depends on what you do to handle the error. If you ever want to know if a function can throw an exception error and why, look up the method on google with java in the line and the oracle documentation will explain all about it.

Thank you Nick, that is very helpful!! I figured since the forum is here I might as well ask the question in the form that was going through my head at the time hah. Thank you again.

Word, that's what it's there for! Anytime mate, happy coding!