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

Philippe Doreau
Philippe Doreau
18,799 Points

Can't pass the last Test (argentinaRecordsAreFormattedAndConverted)

On this exersice, Im totally blocked and can pass the last test, I put the the two functions "usdToArgentinePesoConverter" and "argentineCurrencyFormatter" together in the same function by using "andThen", but after in the return statement I don't know what to do, if I use the new Function gathering "usdToArgentinePesoConverter" and "argentineCurrencyFormatter" I can see the new price in Peso Argentin, but the exercise is not validated by the test, and what is homeValueIndex, and How to use it with the new Function ?

If someone has already did this exercise, I would appreciate some help, Thank you very much

public static List<String> getPricesConvertedForArgentina(List<HousingRecord> records) { Locale argLocale = Locale.forLanguageTag("es-AR"); // This fluctuates and is hardcoded temporarily BigDecimal argPesoToUsdRate = new BigDecimal("15.48");

// TODO: These functions are in working order, but separately they don't match the single Function signature expected
Function<Integer, BigDecimal> usdToArgentinePesoConverter = usd -> argPesoToUsdRate.multiply(new BigDecimal(usd));

Function<BigDecimal, String> argentineCurrencyFormatter = price -> {
  Currency currentCurrency = Currency.getInstance(argLocale);
  NumberFormat currencyFormatter =
    NumberFormat.getCurrencyInstance(argLocale);
  return String.format("%s (%s)",
    currencyFormatter.format(price),
    currentCurrency.getDisplayName()
  );
};

Function <Integer, String> testConverter = usdToArgentinePesoConverter.andThen(argentineCurrencyFormatter);

return getConvertedPriceStatements(records,
  // TODO: Correct this.  It should to convert AND format.  However, there is room parameter wise for only one Function...hmmm.
//homeValueIndex ->
  testConverter
);

}

Philippe Doreau
Philippe Doreau
18,799 Points

I can see this error message:

Expected: iterable containing ["First is $1.00 (USD) which is $15,48 (Argentine Peso)", "Second is $2.00 (USD) which is $30,96 (Argentine Peso)"] but: item 0: was "First is $1.00 (USD) which is $15,48 (peso argentin)"

5 Answers

Fahad Mutair
Fahad Mutair
10,359 Points

hi Philippe Doreau ,i know its not that easy and it took time from me to solve this challenge

here's my code

  public static List<String> getPricesConvertedForArgentina(List<HousingRecord> records) {
    Locale argLocale = Locale.forLanguageTag("es-AR");
    // This fluctuates and is hardcoded temporarily
    BigDecimal argPesoToUsdRate = new BigDecimal("15.48");

    // TODO: These functions are in working order, but separately they don't match the single Function signature expected
    Function<Integer, BigDecimal> usdToArgentinePesoConverter = usd -> argPesoToUsdRate.multiply(new BigDecimal(usd));

    Function<BigDecimal, String> argentineCurrencyFormatter = price -> {
      Currency currentCurrency = Currency.getInstance(argLocale);
      NumberFormat currencyFormatter =
        NumberFormat.getCurrencyInstance(argLocale);
      return String.format("%s (%s)",
        currencyFormatter.format(price),
        currentCurrency.getDisplayName()
      );
    };

    Function<Integer, String> indeedToArgentineCurrencyConverter = usdToArgentinePesoConverter.andThen(argentineCurrencyFormatter);

    return getConvertedPriceStatements(records,
      // TODO: Correct this.  It should to convert AND format.  However, there is room parameter wise for only one Function...hmmm.
      indeedToArgentineCurrencyConverter.andThen(
        (String homeValueIndex) -> String.format(homeValueIndex, homeValueIndex))
    );
  }
Philippe Doreau
Philippe Doreau
18,799 Points

Thank you Fahad for your help, I will try to know to understand the logic : )

You saved me, thanks!

Philippe Doreau
Philippe Doreau
18,799 Points

The code can a be a little bit simpler like below, and it works with the lambda

    Function<Integer, String> argentinConverter = usdToArgentinePesoConverter.andThen(argentineCurrencyFormatter);

    return getConvertedPriceStatements(records,
      // TODO: Correct this.  It should to convert AND format.  However, there is room parameter wise for only one Function...hmmm.
      argentinConverter.andThen(
        homeValueIndex -> homeValueIndex
      )
    );
  }

Moderator Edit: Added Markdown to the code so that it is in a readable format for the Community.

Arran Cardnell
Arran Cardnell
12,313 Points

I was also banging my head against this one for a while. The problem indeed turned out to be the non-breaking space as suggested above, so thank you to Nick for that.

To solve this I modified the getPriceConversionForRecord method as follows:

  public static String getPriceConversionForRecord(HousingRecord record, Function<Integer, String> priceConverter) {
    NumberFormat usFormatter = NumberFormat.getCurrencyInstance(Locale.US);
    return String.format("%s is %s (USD) which is %s",
      record.getRegionName(),
      usFormatter.format(record.getCurrentHomeValueIndex()),
      priceConverter.apply(record.getCurrentHomeValueIndex())
    ).replaceAll("\u00a0", " ");
  }

This replaces all non-breaking spaces with normal spaces, allowing the test to pass.

i followed all steps exactly but i still have the same error : iterable containing ["First is $1.00 (USD) which is $15,48 (Argentine Peso)", "Second is $2.00 (USD) which is $30,96 (Argentine Peso)"] but: item 0: was "First is $1.00 (USD) which is $ 15,48 (Argentine Peso)" Anyone pls help me ?

I'm having the same issue, seems like somehow there's a space between the '$' sign and the integers and that's why the perpareSubmission can't recognize the String as working

Nick Frozz
Nick Frozz
31,289 Points

Actually, it's ($--> <--15,48) not a "space" it's something called "U+00A0 : NO-BREAK SPACE [NBSP]" character. So you can pass this challenge by replacing the code in src/test/com.teamtreehouse.challenges.homes/MainTest

from:

"First is $1.00 (USD) which is $15,48 (Argentine Peso)",

"Second is $2.00 (USD) which is $30,96 (Argentine Peso)"

to:

"First is $1.00 (USD) which is $ 15,48 (Argentine Peso)",

"Second is $2.00 (USD) which is $ 30,96 (Argentine Peso)"