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 trialLuke Horvath
590 Pointscan not return string as int
compiler error;
can not return String as Int
public class Spaceship{
public String shipType;
public int getShipType(){
return "shipType";
}
};
2 Answers
Jason Wiram
42,762 PointsThere are two things you need to fix here.
- Your "getter" method should return the value of the shipType variable. That variable is of type String. That means your method must return a String. Notice the "public String" that tells the method what kind of value to expect. Your method was returning a String but expecting an int. The correct version here:
public String getShipType() {
return "shipType";
}
- Second your return statement is returning a hard-coded String "shipType." It should be returning the value of the shipType variable as follows. This is done by using the variable name (no quotation marks).
public String getShipType() {
return shipType;
}
Tomas Svojanovsky
26,680 PointsHello. You declared shipType as String and you want it as int. In this case return type must be same as type of variable.