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 Objects (Retired) Meet Objects Creating New Objects

Juan Santiago
Juan Santiago
3,766 Points

My code works in "Workspaces" but it doesn't in the final "Challenge Task 2 of 2"

I wrote the code in Workspaces just like Craig in the video (extrapolating it with the GoKart Challenge) and it compiles and runs just fine. But when I paste it in the Challenge Task, the compiler tells me that I made (several) sintax errors. What am I doing wrong D:? HELP!

Example.java
public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
      GoKart k = new GoKart("red");
      System.out.printf("The Go Kart color is %s\n", k.getColor());
    }}

public class GoKart {
  private String mColor;
  public GoKart(String color){
  mColor=color;
  }
  public String getColor(){
   return mColor;
  }
}

2 Answers

Lewis Cowles
Lewis Cowles
74,902 Points

Hey, Treehouse asked me to answer this, so here is what I have...

Example.java:9: error: class GoKart is public, should be declared in a file named GoKart.java

There should be a tab for GoKart.java, which should contain that class, tried this in Workspaces too, so unsure what you tried, but the code could also be formatted a little cleaner

Example.java

import com.lewiscowles.GoKart; // change this to com.yournameoridentifier.GoKart

public class Example {

  public static void main(String[] args) {
    System.out.println("We are going to create a GoKart");
    GoKart k = new GoKart("red");
    System.out.printf("The Go Kart color is %s\n", k.getColor());
  }
}

GoKart.java (please bear in mind I used my own package name)

package com.lewiscowles; // change this to com.yournameoridentifier

public class GoKart {
  private String mColor;
  public GoKart(String color){
    mColor = color;
  }
  public String getColor(){
    return mColor;
  }
}

Hope this helps

Juan Santiago
Juan Santiago
3,766 Points

Thank you so much, your comment brought light to my life XD.

Damien Watson
Damien Watson
27,419 Points

Hi Juan,

The challenge doesn't actually require you to define the GoKart class, so you just need the first half:

public class Example {
    public static void main(String[] args) {
      System.out.println("We are going to create a GoKart");
      GoKart k = new GoKart("red");
      System.out.printf("The Go Kart color is %s\n", k.getColor());
    }
}
Damien Watson
Damien Watson
27,419 Points

Lewis is on the money if you are building this to run on workspaces or your own pc. If you are trying to complete the challenge though, you only need the above.

Lewis Cowles
Lewis Cowles
74,902 Points

Hi Damien, I started writing mine before your comment, you are correct 100%, I was trying to be as full as possible, to why etc, but good answer ;)

Damien Watson
Damien Watson
27,419 Points

Yeh that happens so often... I think they need something like Stack where it updates the page. But still your answer is good for 'real life' ;).

Lewis Cowles
Lewis Cowles
74,902 Points

Meh lol I like how the treehouse forums work tbh, I think they don't need much change (I'd rather they focused on other things like more adv courses, such as the newer Java, PHP, Python courses, just me, but I think thats going to benefit more long-term users)