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 trialMUZ141097 Simangele Jogwe
4,630 PointsFinally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get
Challenge Task 2 of 2
Finally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get a JSONObject from the array. Then use the Log.i() method (with "CodeChallenge" as the tag) to write the show title. Please help am stuck on this challenge. Am getting this error when l run the code below Bummer! Don't forget to use "CodeChallenge" as the 1st parameter of the Log.i() method!
for (int i = 0; i < jsonShows.length(); i++) { JSONObject shows = jsonShows.getJSONObject(i); Log.i("CodeChallenge"); }
// JSONObject was loaded from data.json
JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++) {
JSONObject shows = jsonShows.getJSONObject(i);
Log.i("CodeChallenge");
}
1 Answer
Steve Hunter
57,712 PointsHi there,
The for
loop would look like:
JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++){
JSONObject jObj = jsonShows.getJSONObject(i);
Log.i("CodeChallenge", jObj.getString("title"));
}
Your output isn't what the challenge expects - it wants the TAG and the show title.
Hope that helps.
Steve.
Tuan Zen
6,882 PointsTuan Zen
6,882 PointsHi Steve,
Thanks for your answer!! I'd like to know why u don't need to pass in a return statement for this for loop, 'cause usually there would be a runtime error if there isnt one?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi there,
A
for
loop doesn't need a return to work. This challenge is expecting an output in the form of an entry in theLog
- that's achieved within the loop itself so there's no need to specificallyreturn
anything.Why would a lack of
return
create a runtime error?Steve.