格式化Java MessageFormat文本时出现IllegalArgumentException

问题描述 投票:1回答:3

我给了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)

我已经尝试将货币作为整数返回,如图所示,但也是数字和铸造,但所有返回错误有点类似。使用字符串不会产生问题,只有创建新对象并传递数值。它提供的类型是Object。

编辑我从给定的输入中将单位更改为数字类型。现在我明白了

  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)
java illegalargumentexception
3个回答
1
投票

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

要么将units改为可以转换为数字的东西(例如Integer),要么使用不同的格式来打印它(例如只用{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应该代表什么,因为unitStringcurrencyInteger

看来你已经翻转了两个字段的类型。


-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.