Java MoneyFormatter::parseMoney抛出MoneyFormatter异常

Java MoneyFormatter::parseMoney抛出MoneyFormatter异常,java,joda-money,Java,Joda Money,以这个SSCCE为例(安装了Joda Money library): 我使用MoneyFormatter将字符串打印为字符串。我的(合理的?)期望是可以使用相同的MoneyFormatter将字符串解析回Money对象。但是,唉,没有骰子。它抛出以下错误: Exception in thread "main" org.joda.money.format.MoneyFormatException: Parsing did not find both currency and amount: $ 1

以这个SSCCE为例(安装了Joda Money library):

我使用
MoneyFormatter
字符串打印为
字符串。我的(合理的?)期望是可以使用相同的
MoneyFormatter
将字符串解析回
Money
对象。但是,唉,没有骰子。它抛出以下错误:

Exception in thread "main" org.joda.money.format.MoneyFormatException: Parsing did not find both currency and amount: $ 100.00
    at org.joda.money.format.MoneyFormatter.parseBigMoney(MoneyFormatter.java:237)
    at org.joda.money.format.MoneyFormatter.parseMoney(MoneyFormatter.java:258)
    at test4.Test12.main(Test12.java:35)
所以,我的问题是:如何从
字符串
中获得
货币
对象

编辑:@durron597,你的信息很有用,但没有回答问题。那么,你是如何从
字符串
变成
货币
对象的呢

我添加了删除美元符号的代码:

public static void main(String[] args) {
    BigDecimal bd = new BigDecimal("100");
    MoneyFormatter mf = new MoneyFormatterBuilder().appendLiteral("$ ").appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING).toFormatter();
    String money_as_string = mf.print(Money.of(CurrencyUnit.USD, bd)); // The MoneyFormatter is what printed this string...
    System.out.println(money_as_string);
    Money money = mf.parseMoney(money_as_string.replace("$", "").trim()); // You think it should be able to parse the string it gave me, right?
}
我得到了这个:

Exception in thread "main" org.joda.money.format.MoneyFormatException: Text could not be parsed at index 0: 100.00
    at org.joda.money.format.MoneyFormatter.parseBigMoney(MoneyFormatter.java:233)
    at org.joda.money.format.MoneyFormatter.parseMoney(MoneyFormatter.java:258)
    at test4.Test12.main(Test12.java:35)
因此,它无法解析包含或不包含货币符号的文本


任何人都可以使用
parseMoney()
函数吗?

因此,我首先想到的是,您使用的是
appendLiteral
,而不是
appendCurrencySymbolLocalized
(如果您使用的是
CurrencyUnit.EUR
,您不会想要美元)

但是,当您将其更改为以下内容时:

MoneyFormatter mf = new MoneyFormatterBuilder()
        .appendCurrencySymbolLocalized()
        .appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
        .toFormatter();
您的代码将引发以下异常:

Exception in thread "main" java.lang.UnsupportedOperationException: MoneyFomatter has not been configured to be able to parse
    at org.joda.money.format.MoneyFormatter.parse(MoneyFormatter.java:281)
    at org.joda.money.format.MoneyFormatter.parseBigMoney(MoneyFormatter.java:229)
    at org.joda.money.format.MoneyFormatter.parseMoney(MoneyFormatter.java:258)
    at MoneyTest.main(MoneyTest.java:17)
进一步的研究揭示了这条线索:

附加货币符号本地化

public

将本地化货币符号附加到生成器。 本地化货币符号是由格式化程序的区域设置选择的符号

无法分析符号。

返回:对于链接,此值从不为null

为什么会这样?可能是因为货币符号不明确。以下是一些:

除世界上使用美元或比索的国家外,许多其他国家使用美元符号表示其货币,包括:

  • 尼加拉瓜科尔多瓦(通常写为加元)
  • 萨摩亚语tālā(dollar一词的音译)
  • 汤加帕昂加
  • 津巴布韦(通常写为Z$)

请尝试以下代码:

MoneyFormatter mf = new MoneyFormatterBuilder()
        .appendCurrencyCode()
        .appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
        .toFormatter();
这将生成类似于
USD100.00
print的内容,这些内容将被正确解析。如果您必须使用美元符号,则需要实现自己的打印机/解析器。或者你可以用这个:

import java.io.IOException;
import java.math.BigDecimal;

import org.joda.money.BigMoney;
import org.joda.money.CurrencyUnit;
import org.joda.money.IllegalCurrencyException;
import org.joda.money.Money;
import org.joda.money.format.MoneyAmountStyle;
import org.joda.money.format.MoneyFormatter;
import org.joda.money.format.MoneyFormatterBuilder;
import org.joda.money.format.MoneyParseContext;
import org.joda.money.format.MoneyParser;
import org.joda.money.format.MoneyPrintContext;
import org.joda.money.format.MoneyPrinter;

public class MoneyTest {
    private static enum DollarParserPrinter implements MoneyParser,
            MoneyPrinter {
        INSTANCE;

        private static final String DOLLAR_SYMBOL = "$ ";

        @Override
        public void parse(MoneyParseContext context) {
            int endPos = context.getIndex() + 2;
            if (endPos > context.getTextLength()) {
                context.setError();
            } else {
                String code =
                        context.getTextSubstring(context.getIndex(), endPos);
                if(DOLLAR_SYMBOL.equals(code)) {
                    context.setCurrency(CurrencyUnit.USD);
                    context.setIndex(endPos);
                }
            }
        }

        @Override
        public void print(MoneyPrintContext context, Appendable appendable,
                BigMoney money) throws IOException {
            if(CurrencyUnit.USD == money.getCurrencyUnit()) {
                appendable.append(DOLLAR_SYMBOL);
            } else {
                throw new IllegalCurrencyException("This parser only knows how to print US Dollar money!");
            }
        }
    }

    public static void main(String[] args) {
        BigDecimal bd = new BigDecimal("100");
        MoneyFormatter mf =
                new MoneyFormatterBuilder().append(DollarParserPrinter.INSTANCE, DollarParserPrinter.INSTANCE)
                        .appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
                        .toFormatter();
        String money_as_string = mf.print(Money.of(CurrencyUnit.USD, bd)); 
        System.out.println(money_as_string);
        Money money = mf.parseMoney(money_as_string);
        System.out.println(money);
    }
}

编辑我的答案以回应你。
import java.io.IOException;
import java.math.BigDecimal;

import org.joda.money.BigMoney;
import org.joda.money.CurrencyUnit;
import org.joda.money.IllegalCurrencyException;
import org.joda.money.Money;
import org.joda.money.format.MoneyAmountStyle;
import org.joda.money.format.MoneyFormatter;
import org.joda.money.format.MoneyFormatterBuilder;
import org.joda.money.format.MoneyParseContext;
import org.joda.money.format.MoneyParser;
import org.joda.money.format.MoneyPrintContext;
import org.joda.money.format.MoneyPrinter;

public class MoneyTest {
    private static enum DollarParserPrinter implements MoneyParser,
            MoneyPrinter {
        INSTANCE;

        private static final String DOLLAR_SYMBOL = "$ ";

        @Override
        public void parse(MoneyParseContext context) {
            int endPos = context.getIndex() + 2;
            if (endPos > context.getTextLength()) {
                context.setError();
            } else {
                String code =
                        context.getTextSubstring(context.getIndex(), endPos);
                if(DOLLAR_SYMBOL.equals(code)) {
                    context.setCurrency(CurrencyUnit.USD);
                    context.setIndex(endPos);
                }
            }
        }

        @Override
        public void print(MoneyPrintContext context, Appendable appendable,
                BigMoney money) throws IOException {
            if(CurrencyUnit.USD == money.getCurrencyUnit()) {
                appendable.append(DOLLAR_SYMBOL);
            } else {
                throw new IllegalCurrencyException("This parser only knows how to print US Dollar money!");
            }
        }
    }

    public static void main(String[] args) {
        BigDecimal bd = new BigDecimal("100");
        MoneyFormatter mf =
                new MoneyFormatterBuilder().append(DollarParserPrinter.INSTANCE, DollarParserPrinter.INSTANCE)
                        .appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
                        .toFormatter();
        String money_as_string = mf.print(Money.of(CurrencyUnit.USD, bd)); 
        System.out.println(money_as_string);
        Money money = mf.parseMoney(money_as_string);
        System.out.println(money);
    }
}