格式化Java MessageFormat文本时,抛出IllegalArgumentException

时间:2018-07-12 22:31:14

标签: java illegalargumentexception

我得到了main和TEXT,但是其他所有类和实现都可能是导致错误的原因。

已更新

import java.util.*;
import java.lang.*;
import java.text.*;
import java.math.*;


class BankAccount extends AccountHolder { 
private static final String TEXT =  "I am a {0} account with {1} units of {2} 
currency";

public static void main(String args[] ) throws Exception {

    List<BankAccount> accounts = new ArrayList<BankAccount>();
    accounts.add(new SavingsAccount("USD",3));//Savings
    accounts.add(new SavingsAccount("EUR",2));//Savings
    accounts.add(new CheckingAccount("HUF",100));//Checking
    accounts.add(new CheckingAccount("COP",10000));//Checking
    accounts.add(new BrokerageAccount("GBP",2));//Brokerage
    accounts.add(new BrokerageAccount("INR",600));//Brokerage

    accounts.stream().forEach(
                                account -> System.out.println(
                                    MessageFormat.format(TEXT,
                                        new Object[]{

account.getAccountType().getName(),//make this work
                                        account.getUnits(),//make this work
                                        account.getCurrency()//make this work
                                                       })));
}

AccountHolder customer = new AccountHolder();
String units;
Integer currency;

void setCustomer(AccountHolder acctHolder) {
    this.customer = acctHolder;
}

AccountHolder getAccountType() {
    return customer;
}

//set units
void setUnits(String unit) {
    this.units = unit;
}

void setType(String type) {
    customer.setType(type);
}

String getUnits() {
    return units;
}

//set currency 
void setCurrency(Integer curr) {
    this.currency = curr;       
 }

Integer getCurrency() {
    return currency;
  }
 }

class SavingsAccount extends BankAccount {

SavingsAccount(String unit, Integer curr) {
    super.setName("Saving");
    super.setUnits(unit);
    super.setCurrency(curr);
  } 
 }

class CheckingAccount extends BankAccount {

CheckingAccount(String unit, Integer curr) {
    super.setName("Checking");
    super.setUnits(unit);
    super.setCurrency(curr);
  } 
 }

 class BrokerageAccount extends BankAccount {

 BrokerageAccount(String unit, Integer curr) {
    super.setName("Brokerage");
    super.setUnits(unit);
    super.setCurrency(curr);
}   
}

 class AccountHolder {

 String name;
 String acctType;

String getName() {
    return name;
}   

void setName(String name) {
    this.name = name;
}

void setType(String type) {
    this.acctType = type;
}

String getType() {
    return acctType;
  }
 }

已更新修订

import java.util.*;
import java.lang.*;
import java.text.*;
import java.math.*;


class BankAccount extends AccountHolder { 
private static final String TEXT =  "I am a {0} account with {1,number,#} 
units of {2} currency";

public static void main(String args[] ) throws Exception {

    List<BankAccount> accounts = new ArrayList<BankAccount>();
    accounts.add(new SavingsAccount("USD",3));//Savings
    accounts.add(new SavingsAccount("EUR",2));//Savings
    accounts.add(new CheckingAccount("HUF",100));//Checking
    accounts.add(new CheckingAccount("COP",10000));//Checking
    accounts.add(new BrokerageAccount("GBP",2));//Brokerage
    accounts.add(new BrokerageAccount("INR",600));//Brokerage

    accounts.stream().forEach(
                                account -> System.out.println(
                                    MessageFormat.format(TEXT,
                                        new Object[]{

account.getAccountType().getName(),//make this work
                                        account.getUnits(),//make this work
                                        account.getCurrency()//make this work
                                                       })));
}

AccountHolder customer = new AccountHolder();
Integer units;
String currency;

void setCustomer(AccountHolder acctHolder) {
    this.customer = acctHolder;
}

AccountHolder getAccountType() {
    return customer;
}

//set units
void setUnits(Integer unit) {
    this.units = unit;
}

void setType(String type) {
    customer.setType(type);
}

Integer getUnits() {
    return units;
}

//set currency 
void setCurrency(String curr) {
    this.currency = curr;       
}

String getCurrency() {
    return currency;
 }
}

class SavingsAccount extends BankAccount {

 SavingsAccount(String curr, Integer unit) {
    super.getAccountType().setName("Saving");
    super.setUnits(unit);
    super.setCurrency(curr);
  } 
 }

class CheckingAccount extends BankAccount {

 CheckingAccount(String curr, Integer unit) {
    super.setUnits(unit);
    super.getAccountType().setName("Checking");
    super.setCurrency(curr);
 }  
}

class BrokerageAccount extends BankAccount {

BrokerageAccount(String curr, Integer unit) {
    super.getAccountType().setName("Brokerage");
    super.setUnits(unit);
    super.setCurrency(curr);
    }   
 }

class AccountHolder {

 String name;
 String acctType;

 String getName() {
    return name;
 }   

 void setName(String name) {
    this.name = name;
 }

 void setType(String type) {
    this.acctType = type;
 }

 String getType() {
    return acctType;
 }
}

这是控制台堆栈跟踪:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
    at java.text.DecimalFormat.format(Unknown Source)
    at java.text.Format.format(Unknown Source)
    at java.text.MessageFormat.subformat(Unknown Source)
    at java.text.MessageFormat.format(Unknown Source)
    at java.text.Format.format(Unknown Source)
    at java.text.MessageFormat.format(Unknown Source)
    at BankAccount.lambda$main$0(BankAccount.java:22)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
    at BankAccount.main(BankAccount.java:20)

我尝试将货币作为整数返回,如图所示,但也返回数字,然后进行转换,但都返回类似错误的错误。字符串的使用没有问题,仅与创建新对象并传递数值有关。提供的是对象类型。

修改 我从给定的输入将单位更改为数字类型。现在我得到

  Exception in thread "main" java.lang.NumberFormatException: For input 
 string: 
   "USD"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.valueOf(Unknown Source)
    at SavingsAccount.<init>(BankAccount.java:69)
    at BankAccount.main(BankAccount.java:13)

3 个答案:

答案 0 :(得分:1)

BankAccount.units字段的类型为String,但是您尝试将其显示为具有{1,number,#}格式的数字。

units更改为可以转换为数字的内容(例如Integer),或使用其他格式进行打印(例如仅显示{1}即可显示)。

答案 1 :(得分:1)

您的格式字符串是:

I am a {0} account with {1,number,#} units of {2} currency

MessageFormat.format调用提供值:

new Object[] {
        account.getAccountType().getName(),
        account.getUnits(),    // <== String
        account.getCurrency()  // <== Integer
}

,但鉴于unitcurrency,而unit是{{1},目前尚不清楚Stringcurrency应该代表什么}。

似乎您已经翻转了两个字段的类型。

答案 2 :(得分:-1)

查看字符串格式,单位(1,20,100)应该是整数,货币(USD,INR,GBP)应该是字符串。其余代码看起来不错。