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

Having trouble with code challenge part three in stage four of the Build Your Own Adventure App

"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 mSpaceship, and then set the text for mPassengersField to this value. Note: You will need to convert the int value to a String!"

I'm a little confused on what is being asked here. Here is the code that I have, but I can't get it to work and I'm not really sure what I'm doing wrong:

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

public class LandingActivity extends Activity {

    public Button mThrustButton;
    public TextView mTypeLabel;
    public EditText mPassengersField;

    public Spaceship mSpaceship;
  public String numPass;

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

        mThrustButton = (Button)findViewById(R.id.thrustButton);
        mTypeLabel = (TextView)findViewById(R.id.typeTextView);
        mPassengersField = (EditText)findViewById(R.id.passengersEditText);

        // Add your code here!
      mSpaceship = new Spaceship("FIREFLY");

      mTypeLabel.setText(mSpaceship.getType().toString());
      numPass = mSpaceship.getNumPassengers().toString();
      mPassengersField.setText(numPass);
    }

I'm getting the following errors:

./LandingActivity.java:30: error: reached end of file while parsing
    }
     ^
./LandingActivity.java:28: error: int cannot be dereferenced
      numPass = mSpaceship.getNumPassengers().toString();
                                             ^
2 errors

If anyone could help me out, it would be much appreciated!

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dylan;

This Challenge took me a while as well, let's take a look.

Task

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 mSpaceship, and then set the text for mPassengersField to this value. Note: You will need to convert the int value to a String!

Plan & Produce

We need to set the text for mPassenersField. That sounds like we need to utilize

mPassengersField.setText();

Get the number of passengers for mSpaceship, for that would we use:

mSpaceship.getNumPassengers();

With me so far?

Convert the int value to a String. There are a few ways to do this I am sure, but the way that worked for me was to simply add an empty string to the value. In doing so Java converts the int to a String, so I came up with:

mPassengersField.setText("" + mSpaceship.getNumPassengers());

Probably not the cleanest, more professional approach, but for the life of me I could not get anything else to work. Perhaps it was a result of a late night of coding. :smile:

Happy coding,

Ken

Thank you! That did the trick!

don't forget the semicolon on the second part mSpaceship.getNumPassengers() is should be mSpaceship.getNumPassengers();

Ken Alger Thank you! Thank you! Thank you! I believe I will chalk it up to a long day of coding lessons :) I haven't ever studied Java and am beginning to believe it should precede this Android class.

Thank you for all of your input within the forum!!!

Ken Alger
Ken Alger
Treehouse Teacher

Tina;

Pleased to hear that I was able to assist.

Happy coding,
Ken