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 trialHenry Bown
30,839 PointsUsing the housing dataset, let's produce a menu of all states represented in the data.
Completely stumped on this section of the Introduction to Functional Programming Java course.
public static List<String> getStateCodesFromRecords(List<HousingRecord> records) {
// TODO: Open a stream on records
return records.stream()
// TODO: Map the stream to the state code
.map(HousingRecord::getState)
// TODO: Filter out any records without a state
.filter(Objects::nonNull)
// TODO: There are duplicate state codes in the records, make sure we have a unique representation
.distinct()
// TODO: Sort them alphabetically
.sorted()
// TODO: Collect them into a new list.
.collect(Collectors.toList());
}
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, stateCodes.size())
.mapToObj(i -> String.format("%d. %s ", i, stateCodes.get(i)))
.forEach(System.out::println);
}
As far as I can tell this should all work, but I'm getting these errors
menuIsInclusive
stateCodesIgnoreBlanks
stateCodesAreSorted
The code for the whole project is downloadable from the exercise, if anyone can spot what I'm missing out on I'd really appreciate it.
edit: Solved menuIsInclusive by replacing
IntStream.range(1, stateCodes.size())
.mapToObj(i -> String.format("%d. %s ", i, stateCodes.get(i)))
.forEach(System.out::println);
with
IntStream.rangeClosed(1, stateCodes.size())
.mapToObj(i -> String.format("%d. %s ", i, stateCodes.get(i - 1)))
.forEach(System.out::println);
edit2: Solved stateCodesIgnoreBlanks and stateCodesAreSorted by adding a check for an empty string in addition to checking for null:
.filter(String -> String != null && !String.equals(""))
1 Answer
Henry Bown
30,839 PointsSee edits below question.