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 trialKevin Faust
15,353 Points2 questions regarding what we've done so far
Hi everyone
(1) when we type in nothing, is it considered a string or character? when we type nothing how does it know to go into the 'applyGuess(String letters)' parameter? Why wouldnt it go into the 'applyGuess(char letter)' parameter?
if it went inside the 'char letter' parameter, it wouldnt pass the validation and it would give the error, "A letter is required". but seeing how it always give the error, "no letter found", its always going through the string parameter. Im just curious if nothing is considered a string?
(2) i see no purpose of "boolean isHit = false;" in the prompter file. I checked where we actually used it. the first is when we put our guess in the applyGuess() method. and this will return "isHit" as true if its a hit or as false if it is a miss. and then in the end of our promptForGuess() method, we return that boolean value. I cant see its role in our program. What was the purpose of us using it?
Thanks
1 Answer
Daniel Hartin
18,106 PointsHi Kevin
The call to console.readLine() which is used to grab your input always returns a String, while the difference is subtle the computer recognises them as completely different data types an empty string is defined as "" (double quotes) while an empty char is ''(2 single quotes). This is why the new method always selects the string function because of the data type and the way data types are defined/stored by the computer.
I haven't gone through throughly but yes I think you're right and under normal execution it is unnecessary to assign the boolean variable isHit with any value whatsoever, however it is always best practice to assign variables a placeholder value wherever possible to prevent null value exception errors if for some reason your variable doesn't get assigned a value. so it is simply best practice to give it the value false and to be honest personal preference.
Hope this helps Daniel
Kevin Faust
15,353 PointsKevin Faust
15,353 PointsHi again Daniel :) Thanks for your answer and your help! I understand it aalot better now. and I just wondering if you can confirm if my breakdown is correct:
So when we ask for a input with console.readLine, this will (always) return a string and we will store it in our string variable called guessAsString. so when we run our applyGuess(guessAsString) method, this will always run the applyGuess(String letters) first and then run the applyGuess(char letter) after we change our guess to a character.
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 Pointsyep exactly! when you get onto using a modern IDE (which is my main gripe with these courses) code completion will tell you what the method call will return and makes it much easier to spot errors and plan your code, for now though you will have to rely on the documentation which while thorough means more work!!
Kevin Faust
15,353 PointsKevin Faust
15,353 Pointscleared up alot for me. Thanks alot!!
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 Pointsno problem, when you move to an IDE (which is a little more awkward to setup mind) you will see the benefits and never look back :) (I hated using workspaces so i'm a little biased).