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 trialKayla Flynn
7,652 PointsWhy doesn't the code run correctly when there are commented lines throughout the code using "//"?
Say I have from java data structures question
public class BlogPost implements Comparable<BlogPost>, Serializable {
public int compareTo(BlogPost other) {
//BlogPost other = (BlogPost) obj;
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
This doesn't work. But if I remove // along with that entire line, then it works.
2 Answers
Steve Hunter
57,712 PointsHi Kayla,
There's a couple of pieces in that code that may cause it not to run properly. First, if
has a lowercase i
, not a capital. Second, the condition for the if
statement should be enclosed in parentheses in Java. Something like:
if (x == 0) {
// This does that
return 1;
}
That code is valid and should work. The comment shouldn't influence the code at run time.
Steve.
Darth R3id4k
Courses Plus Student 10,125 PointsI encountered same problems. No comments == no problems. Safari on macOS HS - that's my gear. Maybe some server side parsing problem?
Kayla Flynn
7,652 PointsKayla Flynn
7,652 PointsSorry, that was just a rough example. Every time I'm trying to answer the problem questions and comment something out in an if statement, or class, or anywhere, if Won't run correctly.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsYou may be commenting out a brace or something important? Without seeing the code it's hard to guess, but the double slash negates anything that appears on the rest of that line. You could take out a semi-colon or a closing brace, perhaps?
Next time it happens, paste the code in here and I'll have a look.
Steve.
Kayla Flynn
7,652 PointsKayla Flynn
7,652 PointsOk, I updated my question as an actually example
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsExcellent - thanks. So, you're saying that commenting the code out and leaving the line in makes the code challenge fail but if you delete the whole line, the code passes? I'll try to replicate that.
Kayla Flynn
7,652 PointsKayla Flynn
7,652 PointsExactly!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsMy challenge passed with your code so this might just be a glitch.
There are some issues with your code in terms of the challenge requirements but the compiler can't detect them, for whatever reason. But when I ran your code here with the commented-out line in place, the challenge successfully completed. I just pasted it in and it worked fine.
Kayla Flynn
7,652 PointsKayla Flynn
7,652 PointsThink it's the browser? I usually use Firefox, but I have been using Chrome.
Not a big deal, but it's come up in a couple of challenges now and I would like to keep my comments there just in case the code doesn't pass.