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 trialJoe Smith
429 PointsProblem with code creating object for GoKart color.
My code is attached, I've checked and rechecked many times, and cannot get this to go through, where am I screwing up? Can anyone help?
public class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
GoKart goKart = new GoKart("red");
System.out.printf("goKart,getColor");
}
}
2 Answers
Edith England
4,270 PointsAh you're nearly there.
The problem is that you've put the variable goKart and the method getColor in quotation marks. If you were to run this it would print "goKart, getColor", which isn't what you want. You want it to print "red.".
You also need to add brackets after getColor, as it is a method.
What you're trying to do is use the getColor method (ie getColor()) on the goKart object to get it to tell you that its red.
So:
Change the 5th line from :
System.out.printf("goKart,getColor");
To :
System.out.printf(goKart.getColor());
Oh and I also just realised-you have a comma, I think, after goKart? (my screen is tiny and I can't quite see). If so, you need to change this to a full stop.
HTH
Edith
Kumar Bharath H N
8,324 PointsHi joe,
The problem in your code is with printf statement.
so, in System.out.printf you have to specify the format specifier like %d for int and %s for string in your code when you call goKart.getColor() it returns a string hence you have to use %s as format specifier!. your code will work if you use the following code
public class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
GoKart goKart = new GoKart("red");
System.out.printf("%s",goKart.getColor());
}
}
hope this helps ! Happy coding :)