java tostring 逗号,通过toString方法来显示不带括号和逗号在一个ArrayList信息

I am using a toString method to display data in an arraylist. How do I write the code so that it displays the information without the [ , , ] around my information

see the code below:

case 6:

System.out.println("Enter an account number to view the transactions of the account");

number=keyboard.nextLong();

found=false;

try{

for(int i=0;i

if(aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){

found=true;

System.out.println("Account " + number + ":\tStart Balance: " +money.format(aBank.getAccounts().get(i).getStartBalance()));

System.out.println(aBank.getAccounts().get(i).getTransaction().toString());

System.out.println("Ending Balance :" +money.format(aBank.getAccounts().get(i).getBalance()));

}

}

}

catch (Exception e){

System.out.println("Unable to process request.\n" +e.getMessage());

}

break;

this is the output:

Account 1: Start Balance: $1000.00

[Deposit 1 6/6/2011 $500.00

, Deposit 2 6/6/2011 $489.00

, Deposit 3 6/6/2011 $262.00

, Withdrawal 4 6/6/2011 $897.00

, Withdrawal 5 6/6/2011 $56.32

, Withdrawal 6 6/6/2011 $78.24

]

Ending Balance :$1219.44

notice the brackets and commas that need to be removed

解决方案

Don't use toString() at all. It is not meant to be used to create formatted UI strings. It is a developer tool, not a user presentation tool.

I will add a caveat for the cases where the object represents something with a simple accepted format that can serve both purposes. Java primitive wrappers (like Integer and Float) are an example of this. Other simple classes such as a 2D point can work in this regard as well, but this quickly falls apart as an object becomes more complex.

To do what you are attempting, just create a method to do your display and do the formatting in that method instead.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值