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 trialanjitapanta
2,641 PointsI do not understand what Im doing wrong
Ive been stuck on this forever, I think this is the right code, but I cant get past it.
public class Main {
public static void main(String[] args) {
GoKart kart = new GoKart("yellow");
if (kart.isBatteryEmpty()) {
System.out.println("The battery is empty");
}
kart.drive(2);
try
{
kart.drive(200);
}catch(IllegalArgumentException iae)
{
System.out.println("The error was: %s", iae.getMessage());
}
}
}
2 Answers
Robert Richey
Courses Plus Student 16,352 PointsHi Anjita,
I see a couple issues with your code. The drive method is already being called - don't need to call it again. Wrap your try / catch block around kart.drive(2);
Also, when you catch the error, you're passing in a formatted string to println. Either just print iae
using println()
or change it to printf()
and your formatted string will work.
Kind Regards
anjitapanta
2,641 PointsI did printf, its still not working :(
Robert Richey
Courses Plus Student 16,352 PointsCan I get you to post the line of code that's using printf?
anjitapanta
2,641 Pointsprintf("The error was: %s", iae.getMessage());
Robert Richey
Courses Plus Student 16,352 PointsMake sure System.out is preceding printf
System.out.printf("The error was: %s", iae.getMessage());
If this still doesn't work for you, please post all of the code you're trying, from the challenge.
We'll get through this, I promise!
anjitapanta
2,641 Pointsanjitapanta
2,641 PointsI did printf, its still not working :(