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

Android Build an Interactive Story App The Model-View-Presenter Pattern Creating a Data Model

Luke Horvath
Luke Horvath
590 Points

can not return string as int

compiler error;

can not return String as Int

Spaceship.java
public class Spaceship{

  public String shipType;

  public int getShipType(){
    return "shipType";
  }
};

2 Answers

Jason Wiram
Jason Wiram
42,762 Points

There are two things you need to fix here.

  1. 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";
  }
  1. 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;
  }

Hello. You declared shipType as String and you want it as int. In this case return type must be same as type of variable.