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 Functional Programming Fundamentals Higher Order Function for Price Conversion

Todd Baker
Todd Baker
17,207 Points

Two errors from code: argentinaRecordsAreFormattedAndConverted and creatingPriceConverterWorksForAllCountries

I'm getting two errors for the code challenge and I'm having trouble debugging them. It seems like the errors are caused by discrepancies in the formatting of the currencies, but I'm not sure how to fix that. Can anyone assist?

Below is my current code for the createPriceConverter function:

public static Function<Integer, String> createPriceConverter(Locale locale, BigDecimal usdRate) { // TODO: Examine the hardcoded getArgentinaPriceConverter method // TODO: Using the arguments passed into this method return a function that will work for any locale and rate Function<Integer, BigDecimal> converter = usd -> usdRate.multiply(new BigDecimal(usd));

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

have you checked out this link? Argentina Converter problem

2 Answers

You need to replace characters .replace("\u20ac", "") and .replace("\u00a0", "")

I passed it using the following,

  public static Function<Integer, String> createPriceConverter(Locale locale, BigDecimal usdRate) {
    return usdPrice -> {
      Currency currentCurrency = Currency.getInstance(locale);
      NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
      return String.format("%s (%s)", currencyFormatter.format(usdRate.multiply(new BigDecimal(usdPrice))), currentCurrency.getDisplayName()).replace("\u20ac", "").replace("\u00a0", "");
    };
  }

  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())
    ).replace("\u00a0", "");
  }

These tests need to be rewritten. Above is a hacky way to get it to work, and should not be taken as some kind of best practice.

In case someone was wondering how I knew what to remove. I used the following bit of code to find the offending characters.

    for (char curChar : actual.toCharArray()) {
      System.out.println(curChar + " -> " + String.format("\\u%04x", (int) curChar));
    }
Todd Baker
Todd Baker
17,207 Points

That helped a lot. Thanks! Main take away for me was the replace lines needed to be added to my code.

Todd Baker
Todd Baker
17,207 Points

Yes. Unfortunately, that Argentina Converter Problem addresses the task before the one that I'm stuck on. I'm stuck on the function called createPriceConverter that is supposed to convert any price when provided with the locale and usdRate.