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

Android BlogReader Code Challenge - HashMap variables

I'm having a really tough time understanding what I need to do with this code challenge and could use a hint. It seems no matter what I try, I get the same Compliation Error: "Make sure you use String as both type parameters for the HashMap variables, i.e. <String, String>. Don't forget them in the constructors, too!"

Problem: Inside the β€˜onCreate()’ method, create two HashMap<String, String> variables and in each one, store a website name value using KEY_NAME as the key and the website URL value using KEY_URL as the key.

Code: import android.os.Bundle; import android.widget.SimpleAdapter;

public class WebsiteListActivity extends ListActivity {

public static final String KEY_NAME = "name";
public static final String KEY_URL = "url";

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

    String[] keys = { KEY_NAME, KEY_URL };
    int[] ids = { android.R.id.text1, android.R.id.text2 };

    // Add code here!
    HashMap<String, String> webSite1 = new HashMap<String, String>( KEY_NAME, KEY_URL);
    HashMap<String, String> webSite2 = new HashMap<String, String>( KEY_NAME< KEY_URL);
}

}

4 Answers

Thanks Ben, however even when I changed that webSite2 line so was correct, I still had the same error. I did figure out what I was doing wrong. I shouldn't have been putting the KEY_NAME and KEY_URL as parameters, but instead calling the put method on the variables. Also found by putting my code into eclipse, I was missing 'final' in front of my variable declarations.

String[] keys = { KEY_NAME, KEY_URL }; int[] ids = { android.R.id.text1, android.R.id.text2 };

  // Add code here!
  final HashMap<String, String> webSite1 = new HashMap<String, String>();
  final HashMap<String, String> webSite2 = new HashMap<String, String>();

  webSite1.put(KEY_NAME, "Treehouse");
  webSite1.put(KEY_URL, "http://teamtreehouse.com");
  webSite2.put(KEY_NAME, "Tyranski");
  webSite2.put(KEY_URL, "http://tyranski.com");
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Check out your line for webSite2 - there's a typo! This is something that would normally be caught with an IDE like Eclipse. We have plans to improve the Code Challenge engine and can hopefully add this kind of functionality in the future.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Ah - I missed that in reading your code, too. Glad you got it working! :party:

You don't need the final keyword--it will pass without it. What it means specifically is that those variables can't be reassigned. Maybe an example will help illustrate the difference:

String myName = "Ben";
myName = "Jim"; // OK -- myName now equals "Jim"

...

final String myName = "Ben";
myName = "Jim"; // Ack! This will throw an error in Eclipse: "The final local variable myName cannot be assigned."

This code challenge was a bit confusing especially since I wasn't comfortable with the previous video. It's all a bit confusing and I was like... "What am I supposed to .put in 2nd parameters??" Then I realized it was just to put random strings... :z

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for the feedback - I'll see if I can change the instructions to be a little more clear.