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 Introduction to Functional Programming Reduction and Aggregation Using a Range to Produce a Menu

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Glitch in the testing for prepareSubmission?

I really do love these local challenges, except when something doesn't work. Everything is completed, the build and run produces exactly what it is supposed to, but when I try to run the Gradle prepareSubmission, all of a sudden there are three tests not passing? But the tests that aren't passing are producing the correct output, so the code has to be correct, right? Frustrated as ***, Yep!

Craig Dennis

edit: added failing tests, but shortened to post

com.teamtreehouse.challenges.homes.MainTest > menuIsInclusive FAILED

com.teamtreehouse.challenges.homes.MainTest > stateCodesIgnoreBlanks FAILED

com.teamtreehouse.challenges.homes.MainTest > stateCodesAreSorted FAILED
Craig Dennis
Craig Dennis
Treehouse Teacher

Can I see the method you wrote?

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points
public static List<String> getStateCodesFromRecords(List<HousingRecord> records) {
    return records.stream()
      .map(HousingRecord::getState)
      .filter(p -> p != null) // also tried .filter(Objects::nonNull)
      .distinct()
      .sorted()
      .collect(Collectors.toList());
    // 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.
  }
public static void displayStateCodeMenuDeclaratively(List<String> stateCodes) {
    IntStream.range(1, stateCodes.size())
      .mapToObj(i -> String.format("%d. %s", i, stateCodes.get(i)))
      .forEach(System.out::println);

    // TODO: Use a range to display a numbered list of the states, starting at 1.

  }
Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

When I build and run, the Imperative return has 1 -> 50, but #1 is empty. Mine (declarative) returns 1 -> 49 all sorted.

Craig Dennis
Craig Dennis
Treehouse Teacher

Try using the zero index ( so i-1)

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

If I add 1 to the range and use a zero index, the run output is now the same as the imperative, and now the Submission is producing only 2 errors.

  1. com.teamtreehouse.challenges.homes.MainTest > stateCodesIgnoreBlanks FAILED
  2. com.teamtreehouse.challenges.homes.MainTest > stateCodesAreSorted FAILED
IntStream.range(1, stateCodes.size() + 1)
      .mapToObj(i -> String.format("%d. %s", i, stateCodes.get(i-1)))
      .forEach(System.out::println);

:weary:

Craig Dennis
Craig Dennis
Treehouse Teacher

Range closed no +1....sorry on phone

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

No worries. Feel bad for bothering you, but I REALLY appreciate it. Thank-you very much. :smile:

Changed... Same 2 fails... here's more detailed stack

com.teamtreehouse.challenges.homes.MainTest > stateCodesIgnoreBlanks FAILED
    java.lang.AssertionError: Make sure you to filter out the empty state codes in your 'getStateCodesFromRecords' method
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.assertTrue(Assert.java:41)
        at org.junit.Assert.assertFalse(Assert.java:64)
        at com.teamtreehouse.challenges.homes.MainTest.stateCodesIgnoreBlanks(MainTest.java:139)

com.teamtreehouse.challenges.homes.MainTest > stateCodesAreSorted FAILED
    java.lang.AssertionError: Did you forget to sort the stream in the method 'getStateCodesFromRecords'
    Expected: iterable containing ["AZ", "WY"]
         but: item 0: was ""
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:865)
        at com.teamtreehouse.challenges.homes.MainTest.stateCodesAreSorted(MainTest.java:130)
Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

If you want to look at this during 'business hours' tomorrow, that'll be fine. I don't want to disturb your evening.

4 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Ahhh i see "" is not null but it is empty. ;)

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

That's kind of funny... because I also just figured that out! After sifting through the debug. Haha.

Changed

.filter(p -> p != null) // also tried .filter(Objects::nonNull)

to

.filter(p -> p != "") // also tried .filter(Objects::nonNull)

and everything works. YAY!!!!!!

Thank you so much for taking the time to help me work through that. It is very much appreciated! You rock! :thumbsup:

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

That one really had me banging my head. Learned a great deal from working that though, so... good challenge! :)

You used stateCodes.size() instead of regular integer for the range, you just ended hours of headache for me, thanks Jason lol

hi can anyone explain : .filter(p -> p != "")

Kinda late into the game but instead of filtering p with i just used

!state.isEmpty()
public static List<String> getStateCodesFromRecords(List<HousingRecord> records) {

    return records.stream()
      .map(HousingRecord::getState)
      .filter(state -> !state.isEmpty())
      .distinct()
      .sorted()
      .collect(Collectors.toList());

  }