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 Data Structures - Retired Exploring the Java Collection Framework Lists

Why 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

Hi 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.

Sorry, 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.

You 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.

Ok, I updated my question as an actually example

Excellent - 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.

Exactly!

My 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.

Think 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.

I encountered same problems. No comments == no problems. Safari on macOS HS - that's my gear. Maybe some server side parsing problem?