Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App Finishing the User Interface Using a Model in the Presenter

task 3 of 3 What am i doing wrong?

Finally, notice that our Spaceship class has a new field to store the number of passengers in the ship. Use the method to get the number of passengers for spaceship, and then set the text for passengersField to this value. Note: You will need to convert the int value to a String!

I keep changing everything but i can't seem to find what exactly I am doing wrong.

LandingActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LandingActivity extends AppCompatActivity {

    public Button thrustButton;
    public TextView typeLabel;
    public EditText passengersField;

    public Spaceship spaceship;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing);

        thrustButton = (Button)findViewById(R.id.thrustButton);
        typeLabel = (TextView)findViewById(R.id.typeTextView);
        passengersField = (EditText)findViewById(R.id.passengersEditText);

        // Add your code here!
      spaceship = new Spaceship("FIREFLY");
      typeLabel.setText( spaceship.getShipType());
      String spaceshipText = getString(spaceship.getNumPassengers());
      spaceshipText = String.format(spaceshipText, number);

    }
}
Spaceship.java
public class Spaceship {
    private String shipType;
    private String numPassengers = 0;

    public String getShipType() {
      return shipType;
    }

    public void setShipType(String shipType) {
      this.shipType = shipType;
    }

    public String getNumPassengers() {
      return numPassengers;
    }

    public void setNumPassengers(String numPassengers) {
      this.numPassengers = numPassengers;
    }

    public Spaceship() {
      shipType = "SHUTTLE";
    }

    public Spaceship(String shipType) {
      this.shipType = shipType;
    }
}

3 Answers

Jason Wiram
Jason Wiram
42,184 Points

Let's go step by step.

You are calling the right method with spaceship.getNumPassengers(). But this returns an int. As the directions state, you need to convert the value to a String. Your call to "getString()" will not do that. A quick Google search for "java convert int to String" will give you some ways to solve your problem. Here is one such way.

Integer.toString(spaceship.getNumPassengers())

I don't know what this line of code is attempting to do (especially since no "number" variable exists):

spaceshipText = String.format(spaceshipText, number)

Let's go to the next step though. You need to assign the value from the above "Integer.toString(spaceship.getNumPassengers())" call to the passengersField. The passengersField is an editText so you can use the method "setText" on it. The final line of code looks like this:

passengersField.setText(Integer.toString(spaceship.getNumPassengers()));

That should be all you need to make it work.

Ah, I see what I did wrong now. That extra line was just me trying to make sense of it with the video. Thank you!

Kossivi Selom Afeli
Kossivi Selom Afeli
15,418 Points

Hello , which error are you getting ?

This is all I needed for challenge 3 of 3: Integer.toString(spaceship.getNumPassengers()); passengersField.setText(Integer.toString(spaceship.getNumPassengers()));

Thanks Jason!