gpt4 book ai didi

java - 格式化 Java MessageFormat 文本时出现 IllegalArgumentException

转载 作者:行者123 更新时间:2023-11-30 06:06:05 26 4
gpt4 key购买 nike

我得到了 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)

最佳答案

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

要么更改 units可以转换为数字的东西(例如 Integer )或使用不同的格式来打印它(例如仅使用 {1} 按原样显示)。

关于java - 格式化 Java MessageFormat 文本时出现 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51315346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com