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

Java

Code producing correct out but not passing challenge?

I've been stumped on this for a couple hours and adjusted the code accordingly to match the challenge requirements. The test submission says that I'm failing the menuIsInclusive and the menuStartsAt1 sections though the output I'm receiving on my end seems to say otherwise. Maybe, I'm not writing the way the testSubmission task is looking for?

package com.teamtreehouse.challenges.homes;

import com.teamtreehouse.challenges.homes.model.HousingRecord; import com.teamtreehouse.challenges.homes.service.HousingRecordService;

import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.OptionalInt; import java.util.stream.Collectors; import java.util.stream.IntStream;

public class Main { public static void main(String[] args) { HousingRecordService service = new HousingRecordService(); List<HousingRecord> records = service.getAllHousingRecords();

List<String> stateCodes = getStateCodesFromRecords(records);
System.out.println("Imperatively:");
displayStateCodeMenuImperatively(stateCodes);
System.out.println("Declaratively:");
displayStateCodeMenuDeclaratively(stateCodes);

}

public static List<String> getStateCodesFromRecords(List<HousingRecord> records) { // TODO: Open a stream on records // TODO: Map the stream to the state code // TODO: Filter out any records without a state // TODO: There are duplicate state codes in the records, make sure we have a unique representation // TODO: Sort them alphabetically // TODO: Collect them into a new list. return records.stream() .map(HousingRecord::getState) .filter(state -> !state.isEmpty()) .distinct() .sorted() .collect(Collectors.toList()); }

public static void displayStateCodeMenuImperatively(List<String> stateCodes) { for (int i = 0; i < stateCodes.size(); i++) { System.out.printf("%d. %s %n", i + 1, stateCodes.get(i)); } }

public static void displayStateCodeMenuDeclaratively(List<String> stateCodes) { // TODO: Use a range to display a numbered list of the states, starting at 1. IntStream.range(1, 50) .mapToObj(i -> String.format("%d. %s", i, stateCodes.get(i-1))) .forEach(System.out::println); }

public static int getLowestHomeValueIndexImperatively(List<HousingRecord> records) { int lowest = -1; for (HousingRecord record : records) { int index = record.getCurrentHomeValueIndex(); if (lowest == -1) { lowest = index; } else { if (index < lowest) { lowest = index; } } } return lowest; }

public static OptionalInt getLowestHomeValueIndexDeclaratively(List<HousingRecord> records) { return records.stream() .mapToInt(HousingRecord::getCurrentHomeValueIndex) .min(); }

public static HousingRecord getHighestValueIndexHousingRecordImperatively(List<HousingRecord> records) { HousingRecord maxRecord = null; for (HousingRecord record : records) { if (maxRecord == null) { maxRecord = record; } else { if (record.getCurrentHomeValueIndex() > maxRecord.getCurrentHomeValueIndex()) { maxRecord = record; } } } return maxRecord; }

public static Optional<HousingRecord> getHighestValueIndexHousingRecordDeclaratively(List<HousingRecord> records) { return records.stream() .max(Comparator.comparingInt(HousingRecord::getCurrentHomeValueIndex)); } }

3 Answers

I had this same problem. For the housing records, all 50 states aren't necessarily represented, so if you have "50" for the upper limit of your range in displayStateCodeMenuDeclaratively, you will receive an OutOfBoundsException for one of the unit tests (for me, it was the menuIsInclusive test).

Thus, set the upper limit of the range to be stateCodes.size().

Hope this helped!

for menuIsInclusive error put IntStream.rangeClosed(1, stateCodes.size()) .mapToObj(i -> String.format("%d. %s", i, stateCodes.get(i-1))) .forEach(System.out::println); .also hard a hard time on that part.

rangeClosed() is the one for menu to be inclusive.

Thanks!